Reputation: 19682
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
Upvotes: 2
Views: 535
Reputation: 5050
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.
Upvotes: 3
Reputation: 6037
This is new to objective-C it turns the primitive integer into an NSNumber, there are also equivalents for NSArrays @(..) and NSDictionary @{...}
Upvotes: 1
Reputation: 43472
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.
Upvotes: 1