Syntax for accessing instance variables? (Objective-C)

What is the correct syntax for accessing an instance variable in Objective-C?

Suppose we have this variable:

@interface thisInterface : UIViewController {
    NSMutableString *aString;
}

@property (nonatomic, retain) NSMutableString *aString;

and that it is synthesized.

When we want to access it, we first want to select and initialize it. By querying Objective-C for about a month, I saw two different forms of syntax. I saw people just aString = [[NSMutableString alloc] initWithString:@"hi"]where they highlight such a line; I also saw how people started it with help self.aString, and then they started to initialize their ivar. I guess I'm just trying to figure out what is the most correct way to initialize an instance variable, because in the first example I got EXC_BAD_ACCESS errors from it. After the addition self., however, it did not appear.

Forgive me if this is a duplicate question, but after reading some posts on SO, I was curious. I am trying to learn the correct syntax using Objective-C, because I prefer to be correct, rather than sloppy.

+3
source share
7 answers

If you also declared the property @synthesizein the .m file, you simply set it like this:

self.aString = @"hi"; // or [[NSMutableString alloc] initWithString:@"hi"];

Usage self.varNameuses what the declaration of your property actually does - it handles saving the new value (since your property has an attribute retain), freeing the old value for you, etc.

If you just do:

aString = someValue;

... , aString, self.aString .

+13

( ) OK BAD , aString retain:

@property (nonatomic, retain) NSMutableString *aString;

:

1

aString = [[NSMutableString alloc] init]; //OK: 

, , aString , , , .

2

aString = [NSMutableString string]; //BAD

, aString ( ), , EXC_BAD_ACCESS

3

aString = [[NSMutableString string] retain]; //OK

, , , aString . , .

4

aString = [[[NSMutableString alloc] init] autorelease];//BAD

, .

5

self.aString = [[NSMutableString alloc] init]; //BAD!!

, , ,

6

self.aString = [[NSMutableString string]; //******GOOD!******

, , . , , , aString,

7

self.aString = [[NSMutableString string] retain]; //BAD

.

8

self.aString = [[[NSMutableString alloc] init] autorelease];//Ok

, :)

, №1 №3 , , . , # 6

+7

self->varName self.varName

- . - .

? . , ( @synthesized ). , , @synthesized accessors (.. self.varName = ...;), varName = ...; , , .. → ( EXC_BAD_ACCESS ).

. , self->varName, self.varName, @property.

+7

self.. , , , NSAutoreleasePool . , EXC_BAD_ACCESS, , self.. , , , .

, .

+2

, init, dealloc . , , . , ivars - , (_foo, foo_, mFoo, foo).

self.foo [self foo]. foo. self.foo = x a [self setFoo:x]. setFoo:. foo retain, :

@synthesize foo = foo_;

- (void)setFoo:(id)value {
  [value retain];
  [foo_ release];
  foo_ = value;
}

foo_, .

foo = x (, foo ivar) . . x foo. foo , , . , , , .

- , .

+2

.

( ), . self.iVar [self iVar], self.iVar = aValue [self setIVar:aValue];

+1

self.aString [self aString]. , -aString -setAString: ( , , ).

, .. . ? , Objective-C C. , C- Objective-C.

, . . C Objective-C.

, , . [self..].

0

All Articles