objective-c - NSArray 上的 IOS 类别导致 "instance message does not declare a method with selector"

标签 objective-c ios cocoa-touch categories

我正在尝试使用本文中描述的类别: http://iphonedevelopment.blogspot.com/2008/10/shuffling-arrays.html

我已经设置了以下内容:

//  NSArray+Shuffle.h
#import <Foundation/Foundation.h>
@interface NSArray (Shuffle)
-(NSArray *)shuffledArray;
@end


//  NSArray+Shuffle.m
#import "NSArray+Shuffle.h"
@implementation NSArray (Shuffle)
-(NSArray *)shuffledArray
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
    NSMutableArray *copy = [self mutableCopy];
    while ([copy count] > 0)
    {
        int index = arc4random() % [copy count];
        id objectToMove = [copy objectAtIndex:index];
        [array addObject:objectToMove];
        [copy removeObjectAtIndex:index];
    }
// Using IOS 5 ARC
//    [copy release];
return array;
}
@end

然后在我想使用它的代码中,我导入了类别:

#import "NSArray+Shuffle.h"

然后,我尝试这样使用它:

    NSArray *orderedGallary = [[NSArray alloc] initWithObjects:
                 [NSDictionary dictionaryWithObjectsAndKeys:
                  @"Pic1", @"pageName",
                  [UIImage imageNamed:@"Pic1.jpg"],@"pageImage",
                  nil],
                 [NSDictionary dictionaryWithObjectsAndKeys:
                  @"Pic2", @"pageName",
                  [UIImage imageNamed:@"Pic2.jpg"],@"pageImage",
                  nil],

                 nil];   

    NSArray *shuffler = [[NSArray alloc] shuffledArray:orderedGallary];

    _pageData = [shuffler shuffledArray:orderedGallary];

但是,我收到以下编译器错误消息:

ModelController.m: error: Automatic Reference Counting Issue: Receiver type 'NSArray' for instance message does not declare a method with selector 'shuffledArray:'

有什么想法吗?

最佳答案

shuffledArray 是一种不带参数的方法,它不同于shuffledArray:,后者是带一个参数的方法。

看起来你的意思是:

NSArray* shuffled = [orderedGallery shuffledArray];

在这里,您将这条消息发送到您的原始数组,它返回一个经过混洗的新数组。

关于objective-c - NSArray 上的 IOS 类别导致 "instance message does not declare a method with selector",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8685743/

相关文章:

iOS - 具有可能 subview 的不同组合的动态 UI

ios - UITextField 字符限制

objective-c - 如何从应用程序委托(delegate)访问 View Controller 变量......反之亦然?

ios - 使用 AFHTTPRequestOperationManager 的子类时无法设置 timeoutInterval

ios - iPhone : locationManagerShouldDisplayHeadingCalibration ignored/does nothing

iphone - Xcode 调试器不停止 i ipod 与 IOS 5 Beta

objective-c - 从 Objective-C cocoa 调用 Perl 库

ios - 针对 ios6.1 编译的 Xcode 5 不保留布局

iphone - 通过调用 +class 有条件地为 iOS 6 和 7 编码是否安全?

iOS:即使路径正确,Twitter Stream API 仍为 Expanded_URL 返回 Null?