how to declare variable of NSString with double pointer
Asked Answered
B

1

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.

Byrdie answered 28/2, 2013 at 22:14 Comment(7)
The code you've shown is all totally legal. Without more context, I don't think we can answer your question.Bohlin
@CarlNorum Except when Automatic-"smartass"-reference-counting comes into the image... Then it makes the compiler complain about each and every small momentum...Limonene
Oh I see... forgot about that. ARC is newer than when I stopped writing any Objective-C code.Bohlin
@CarlNorum You're lucky... Can we agree that the answer here should be "turn off ARC"?Limonene
Or try with this, NSString *__strong *a;Oballa
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.Microscope
Spot on. +1 to whatever @Microscope says.Oballa
O
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.

Oballa answered 28/2, 2013 at 22:24 Comment(1)
ive seen a bunch of objc code with Type** name declarations. Has that been deprecated?Midden

© 2022 - 2024 — McMap. All rights reserved.