1

I'm a bit confused with a tutorial I'm studying. There is a line of code that look like this:

location.photoId  = @([Location nextPhotoId]);

Im not understand meaning of @() syntax, what is it? There is definition of variables used in this statement:

@interface Location : NSManagedObject <MKAnnotation>
 Location *location = nil;

@property (nonatomic, retain) NSNumber *photoId;

Declared in Location class.

+(NSInteger)nextPhotoId{

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSInteger photoId = [defaults integerForKey:@"PhotoId"];
                         [defaults setInteger:photoId+1 forKey:@"PhotoId"];
                         [defaults synchronize];
                         return photoId;
}

Class method in Location class.

Maybe its not necessary to paste that code here, but I think I should make it clearer. I only wish to know what @([Location nextPhotoId]) mean in that case, and what @() stand for?

Any help would be appreciated!

Nayan
  • 3,014
  • 2
  • 17
  • 33
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
  • 2
    `@()` is universal syntax for creating object literals. `@(some_integer)` and `@(some_floating_point)` create `NSNumber` objects, `@(some_char_pointer)` create `NSString`s, and `@[]` and `@{}` create `NSArray` and `NSDictionary` literals, respectively. –  Jan 16 '14 at 10:20
  • 2
    possible duplicate of - http://stackoverflow.com/questions/21084156/what-does-mean – Nayan Jan 16 '14 at 10:21
  • 1
    `@([Location nextPhotoId])` is just equivalent to `[NSNumber numberWithInt:[Location nextPhotoId]];` – Deepak Bharati Jan 16 '14 at 10:32

1 Answers1

3

@([Location nextPhotoId]) is just equivalent to [NSNumber numberWithInt:[Location nextPhotoId]]

@() is syntax used to define a literal.

Method [Location nextPhotoId] returns an integer, and to set this integer as NSNumber (location.photoId) this syntax is used.

Reference: http://cocoaheads.tumblr.com/post/17757846453/objective-c-literals-for-nsdictionary-nsarray-and

Deepak Bharati
  • 280
  • 2
  • 13