2

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

Ali
  • 18,665
  • 21
  • 103
  • 138

3 Answers3

3

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.

Paul Lalonde
  • 5,020
  • 2
  • 32
  • 35
1

It's a new syntax for boxing values with less typing. Assuming mt[i] is a numeric type, @(mt[i]) places it in an NSNumber object.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
1

This is new to objective-C it turns the primitive integer into an NSNumber, there are also equivalents for NSArrays @(..) and NSDictionary @{...}

Nathan Day
  • 5,981
  • 2
  • 24
  • 40