32199

NSString alloc/init generates 'Redefinition of myStr' error…?

Question:

I seem to be generating a lot of questions with this one little iPhone app....

I am attempting to stuff an NSMutableArray with NSStrings, and I am working like this:

... NSString *myStr = [[NSString alloc] initWithString:@"Jupiter II"]; [txtArray addObject: myStr]; [myStr release]; NSString *myStr = [[NSString alloc] initWithString:@"Could this be OUR Waffles?"]; // in this second block, the line above generates the error: 'Redefinition of myStr'. Why? [txtArray addObject: myStr]; [myStr release]; ...

The first block generates no errors, but the subsequent blocks (identical save for the content of each string object) all generate the error, 'Redefinition of myStr'. Why?

I know that this is going to be a facepalm, but I just gotta know, and get beyond this!

hanks for any and all assistance!

Kind regards,

Steve O'Sullivan

Answer1:

Just remove NSString* from the second block:

NSString *myStr = [[NSString alloc] initWithString:@"Jupiter II"]; [txtArray addObject: myStr]; [myStr release]; myStr = [[NSString alloc] ...

Each time you write something like NSString *myStr you declare new variable - and variable names must be different in the same scope. But you can reuse the same variable to store different objects - as you do in the code above

Answer2:

Maybe it was just for the sake of example, but your first three lines of code can be substituted with:

[txtArray addObject:@"Jupiter II"]

Also, creating an array of strings is as simple as:

[NSArray arrayWithObjects:@"Jupiter II", @"Could this be OUR Waffles", nil];

Recommend

  • sum of count in xslt-3
  • UItableviewcell background color while click
  • C++ Boost ptr_map serialization error
  • Usage of local dart library
  • Formatting a text in a table cell with PHPWord e.g. bold, font, size e.t.c
  • insert a picture into database(sqlite) with java code. what should i do?
  • HighCharts - Show tooltip on column where value is 0 or null
  • docx4j replace variable with html
  • c# winform DrawToBitmap offscreen
  • distinct values from multiple fields within one table ORACLE SQL
  • How to validate a year I enter in textbox using jquery rule?
  • unrecognized selector with category NSMutableData
  • Regex for Specific Tag
  • WordPress > setting permalink option via script buggy?
  • triggering user space with kernel
  • Responsive left sidebar open close
  • Python PIL to extract number from image
  • Prevent page break in text block with iText, XMLWorker
  • Updating product post meta data in admin meta box field
  • Less Conflicting Session Manager for Zope 2
  • Cannot save model when using ember render helper
  • C++ friend class std::vector
  • C#: Import/Export Settings into/from a File
  • Why does java tzupdater add leap seconds?
  • SyntaxError: (irb):26: both block arg and actual block given
  • What is the purpose of TaskExecutor in spring?
  • How can I set a binding to a Combox in a UserControl?
  • AJAX Html Editor Extender upload image appearing blank
  • Moving mysql files across servers
  • Row Count Is Returning the incorrect number using RaptureXML
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Trying to switch camera back to front but getting exception
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • -fvisibility=hidden not passed by compiler for Debug builds
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass