Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <pre><code>NSString *fname = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); NSMutableString *lname = (NSMutableString *)ABRecordCopyValue(person, kABPersonLastNameProperty); </code></pre> </blockquote> <p>What makes you think <code>ABRecordCopyValue</code> is going to return a mutable string here?</p> <p>Just telling the compiler that it will return a mutable string (which is all “<code>(NSMutableString *)</code>” does) doesn't mean it will. If the Address Book documentation doesn't specify that this will return a mutable string, assume it won't and create a mutable string yourself.</p> <p>Speaking of which…</p> <blockquote> <pre><code>NSMutableString *name = [[NSMutableString alloc] init]; </code></pre> </blockquote> <p>Here's the string you should be appending to. You don't need <code>lname</code> to be mutable, because this string is mutable.</p> <blockquote> <pre><code>NSMutableString *space = @" "; fname = [fname stringByAppendingFormat:space]; fname = [fname stringByAppendingFormat:lname]; </code></pre> </blockquote> <p>By doing this, you waste the mutable string you created. You are creating two intermediate immutable strings here, not appending to the mutable string.</p> <blockquote> <pre><code>name = fname; </code></pre> </blockquote> <p>And here, you throw away the mutable string entirely (and thereby leak it since you never released it), replacing it with the immutable string you got from your series of <code>stringByAppendingFormat:</code> messages.</p> <p>What you should do is send the <code>name</code> mutable string an <code>appendFormat:</code> message, passing both the first and last name strings. You don't need the <code>space</code> string; you can include that in the format string.</p> <p>See also <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSMutableString/appendFormat:" rel="nofollow noreferrer">the NSMutableString docs</a>.</p> <blockquote> <pre><code>[nameArray addObject:fname]; </code></pre> </blockquote> <p>At no point prior to this statement have you created an NSMutableArray object and stored its pointer in the <code>nameArray</code> variable. Not in any code you've shown, anyway.</p> <p>Until you do that, this variable holds <code>nil</code>, the pointer to no object. The <code>addObject:</code> message does nothing because that's what messages to <code>nil</code> do: Nothing. Logging the array you don't have produces “(null)” because that's the description of <code>nil</code>.</p> <blockquote> <pre><code> if (nameArray == nil) { NSLog(@"NO DATA TO RETRIEVE FROM USERDEFAULTS"); </code></pre> </blockquote> <p>You aren't showing any code that retrieves from user defaults. Even if you did, it would return an immutable array as Sven said; you would have to make a mutable copy.</p> <p>This is the best I can do without a description of the problem. We may be able to provide more and better suggestions if you edit your question to tell us what happens when you run the above code, and not just what doesn't happen.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload