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

有帮助吗?

解决方案

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.

其他提示

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.

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top