Search code examples
objective-cat-sign

What does the "at sign" @ mean in this objective C code


I have been using Objective C for quite a few years but I didn't know @ sign can be used like this (line 6 inside the for loop):

- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeInteger:mti forKey:@"mti"];

    NSMutableArray *arr = [NSMutableArray arrayWithCapacity:N];
    for (int i = 0; i < N; i++)
        [arr addObject:@(mt[i])];
    [coder encodeObject:arr forKey:@"mt"];
}

What does it mean? surprisingly I can remove it and the compiler does not complain and the code looks like working fine?!

This is part of MTRandom https://github.com/preble/MTRandom/blob/master/MTRandom/MTRandom.m#L115


Solution

  • In this context, the @ operator converts a C numeric value (int, long, float, double, etc) into an instance of NSNumber. It's most often used with numeric literals (eg @3.5), but also applies to expressions as in your example.

    This enhancement to the Objective-C language was introduced with Xcode 4.4.