6

I wanna use double pointer and I tried to declare like this.

NSString **a;

but, Xcode showed me the error "Pointer to non-const type 'NSString *' with no explicit ownership" and it couldn't be compiled.

Finally I wanna do like this.

NSString **a;
NSString *b = @"b";
NSString *c = @"c";
a = &b;
*a = c;

NSLog(@"%@",b);//I wanna see "c"

Let me know any advise please.

iDev
  • 23,310
  • 7
  • 60
  • 85
Takuya Takahashi
  • 453
  • 1
  • 4
  • 16
  • The code you've shown is all totally legal. Without more context, I don't think we can answer your question. – Carl Norum Feb 28 '13 at 22:19
  • 1
    @CarlNorum Except when Automatic-"smartass"-reference-counting comes into the image... Then it makes the compiler complain about each and every small momentum... –  Feb 28 '13 at 22:19
  • Oh I see... forgot about that. ARC is newer than when I stopped writing any Objective-C code. – Carl Norum Feb 28 '13 at 22:20
  • @CarlNorum You're lucky... Can we agree that the answer here should be "turn off ARC"? –  Feb 28 '13 at 22:20
  • Or try with this, `NSString *__strong *a;` – iDev Feb 28 '13 at 22:23
  • 2
    Note that using `**`s to refer to objects is really quite rare in Objective-C. Also, @H2CO3, one of the goals of ARC was to make uncommon, fragile, coding patterns (that often cause bugs) quite precise in declaration. This is one of those cases. "Turn off ARC" is not a constructive suggestion; many of us have fully embraced ARC and have seen a drastic reduction in code fragility because of it exactly because the compiler is able to be significantly more thorough in its analysis. – bbum Mar 01 '13 at 00:25
  • Spot on. +1 to whatever @bbum says. – iDev Mar 01 '13 at 05:55

1 Answers1

12

Change to this so that you can explicitly specify the ownership:

NSString *__strong *a;
NSString *b = @"b";
NSString *c = @"c";
a = &b;
*a = c;

NSLog(@"%@",b);//I wanna see "c"

Output:

 c

Here is the documentation on __strong.

iDev
  • 23,310
  • 7
  • 60
  • 85