开发者

Obj-c dot notation for reading properties: why can't i combine it like this?

i have this line of code:

[[[EntitlementsManager instance] entitlements] objectAtIndex:indexPath.row];

Why won't it work in the way below:

[EntitlementsManager.instance.entitlements objectAtIndex:indexPath.row];

Seems like it should work to me? I'm a little confused as to why it doesn't compile.

FYI, EntitlementsManager, 'instance' is the '+' method to return its singleton, and 'entitlem开发者_高级运维ents' is an NSArray property.

-edit: For those why say it doesn't work because 'instance' is a static '+' method, then why does the following work just fine? I'm really intrigued:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return EntitlementsManager.instance.entitlements.count;
}

-edit2: This does work, strangely:

... = [[EntitlementsManager instance].entitlements objectAtIndex:indexPath.row];


Possibly the parser got confused when it sees a Type appearing on the left of a . inside an Obj-C method call. For example, if the grammar

'[' Type   methodAndArguments ']'

exists, then it will be parsed before recognizing a '.' b, and since .instance ... isn't an expected construct of methodAndArguments, the compiler will fail. As Type.method isn't one expected use of the dot syntax, it is valid for the compiler not to support it, or even making its use a syntax error in the future.

Either always use the bracket notation for class methods as you're expected to:

[[EntitlementsManager instance].entitlements objectAtIndex:0]

or move that expression outside of bracket:

NSArray* entitlements = EntitlementsManager.instance.entitlements;
[entitlements objectAtIndex:0];

or force that '[' Type methodAndArguments ']' not to be matched:

[(nil, EntitlementsManager.instance.entitlements) objectAtIndex:0]


Try this way

[(EntitlementsManager.instance.entitlements) objectAtIndex:indexPath.row];

might work

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜