Compiler error “expected method not found” when using subscript on NSArray

后端 未结 7 1888
悲哀的现实
悲哀的现实 2020-11-29 04:45

I wrote this simple code to try out the new Objective-C literal syntax for NSArrays:

NSArray *array = @[@"foo"];
NSLog(@"%@",         


        
相关标签:
7条回答
  • 2020-11-29 05:00

    I got the same problem, but then it wasn't b/c of a lacking iOS version.. but it was simply because the original array was set as an NSArray rather than an NSMutableArray. Changing it to NSMutableArray fixed it for me

    0 讨论(0)
  • 2020-11-29 05:02

    If anyone gets to this old thread after getting this error in Xcode 9.3 beta 4 with some legacy Objective-C code like I did, here was my fix.

    Update:

    @property (nonatomic, strong) id<CustomClass> myObject;
    

    To:

    @property (nonatomic, strong) NSMutableArray<CustomClass> *myObject;
    
    0 讨论(0)
  • 2020-11-29 05:06

    I got the following code from this link:

    You can just add this category to NSObject and collection subscripting will work. I put it in my .pch file.

    // Add support for subscripting to the iOS 5 SDK.
    #if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000
    @interface NSObject (SubscriptingSupport)
    
    - (id)objectAtIndexedSubscript:(NSUInteger)idx;
    - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
    - (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
    - (id)objectForKeyedSubscript:(id)key;
    
    @end
    #endif
    

    Of course you will need the newest version of CLANG which is already in XCode 4.4.

    0 讨论(0)
  • 2020-11-29 05:08

    If you're not targeting iOS 6 or OS X 10.8, I would like to point out that it's still remarkably easy to get subscripting to work. All you have to do is add the required methods as a category the classes you want subscripting to work for, and implement those methods appropriately. So add to the following classes the methods:

    NSArray : - (id)objectAtIndexedSubscript: (NSUInteger)index;

    NSMutableArray : - (void)setObject: (id)obj atIndexedSubscript: (NSUInteger)index;

    NSDictionary : - (id)objectForKeyedSubscript: (id)key;

    NSMutableDictionary : - (void)setObject: (id)obj forKeyedSubscript: (id)key;

    Implementing this is a simple as calling the appropriate method for the class. For example, to implement subscripting on NSArray you just implement:

    - (id) objectAtIndexedSubscript:(NSUInteger)index{
        return [self objectAtIndex:index];
    }
    

    The only downside I can see is you need to make sure to import your category into any class that intends on using the subscripting. Of course, you can get around that requirement by including the #import in your prefix header, usually the file: <appname>-Prefix.pch. (thanks Josh Caswell for pointing that out).

    One upside is you can alter the subscripting methods to suit your needs. For example, Apple doesn't allow you to add/remove objects to NSMutableArray using subscripting, but this can be accomplished easily enough:

     - (void) setObject:(id)obj atIndexedSubscript:(NSUInteger)index{
        if (index < self.count){
            if (obj)
                [self replaceObjectAtIndex:index withObject:obj];
            else
                [self removeObjectAtIndex:index];
        } else {
            [self addObject:obj];
        }
    }
    
    0 讨论(0)
  • 2020-11-29 05:10

    I'm adding this because this is a common error that still exists in Xcode as of 7/2015 and it's not easy to figure out how to resolve it.

    I received this error when attempting to call a method on an object without having created an instance of the object. My solution was to create an instance of the object, then call the method on the property on the instance of that object.

    Example: What didn't work: [self methodCall:arrayItem] (see full example below)

    [self tappedUser:self.activities[indexPath.row].followItem.user.givenName];
    

    What fixed it: ObjectClass newObject = arrayItem; [self methodCall:newObject] (see full example below)

    FollowActivityItem *followItem = self.activities[indexPath.row];
    [self tappedUser:followItem.user.givenName];
    
    0 讨论(0)
  • 2020-11-29 05:17

    add #import <Foundation/Foundation.h> to your h file, and use an NSMutableArray instead

    0 讨论(0)
提交回复
热议问题