我正在尝试使用本文中描述的类别: 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/