jspahrsummers / libextobjc

A Cocoa library to extend the Objective-C programming language.
MIT License
4.53k stars 461 forks source link

@keypath([self new], someProperty) not work? #117

Closed CodeLife2012 closed 6 years ago

CodeLife2012 commented 8 years ago

Sometimes may want to autofill a object in @keypath(object, property), but we can't use [self new] or[[self alloc] init] because xCode treat these object aid type.

Actually it's not a bug in this library, but anyone has a workaround for this?

jspahrsummers commented 8 years ago

-init and +new should be instancetype, not id, which means that dot syntax should compile.

CodeLife2012 commented 8 years ago

@jspahrsummers

[SomeClass new] can work, but in a class method[self new]or [[self alloc] init]is not work.

Maybe it's a bug in Xcode.

jspahrsummers commented 8 years ago

In that case, just use the class' name within the class method too:

@implementation SomeClass

+ (void)foo {
  … @keypath([SomeClass new], property) …
}
k06a commented 8 years ago

I am usually using it this way:

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    ResultObject *this = nil;
    return @{
        @keypath(this.url) : @"object.url",
        @keypath(this.summaryUrl) : @"object.extensions.summaryUrl",
        @keypath(this.title) : @"object.title",
        @keypath(this.favIconUrl) : @"faviconUrl",
        @keypath(this.backgroundImageUrl) : @"backgroundImageUrl",
        @keypath(this.fallbackImageUrl) : @"fallbackImageUrl",
        @keypath(this.backgroundImageSourceUrl) : @"backgroundImageSourceUrl",
    };
}
CodeLife2012 commented 8 years ago

@k06a looks great