Iphone 需要帮助的新手解数组
我发现这个帖子: 当我试图在自己的代码中部署它时,我无法让它工作 有人能帮我解决这个问题吗 在我看来,似乎没有调用洗牌函数 这是我的密码:Iphone 需要帮助的新手解数组,iphone,objective-c,cocoa,cocoa-touch,shuffle,Iphone,Objective C,Cocoa,Cocoa Touch,Shuffle,我发现这个帖子: 当我试图在自己的代码中部署它时,我无法让它工作 有人能帮我解决这个问题吗 在我看来,似乎没有调用洗牌函数 这是我的密码: // // shuffle2ViewController.h // shuffle2 #import @interface shuffle2ViewController : UIViewController { NSMutableArray *puzzles; int *randomSort; } - (void)shuffle; @end //=
// // shuffle2ViewController.h // shuffle2
#import
@interface shuffle2ViewController : UIViewController {
NSMutableArray *puzzles;
int *randomSort;
}
- (void)shuffle;
@end
//=============================
// shuffle2ViewController.m
´#import "shuffle2ViewController.h"
@implementation shuffle2ViewController
(void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *puzzles = [NSMutableArray arrayWithObjects:@"1",@"2",@"3", @"4",@"5",@"6",@"7",@"8",@"9", @"10",@"11",@"12", nil];
// Call the shuffle function
[self shuffle];
// print to log
int i;
NSLog(@"NEW OLD");
NSLog(@"=================");
for (i = 0; i < 12; ++i) NSLog(@" %2i %@", i + 1, [puzzles objectAtIndex:i]); }
int randomSort(id obj1, id obj2, void *context ) {
// returns random number -1 0 1
return (random()%3 - 1); }
(void)shuffle { // call custom sort function
[puzzles sortUsingFunction:randomSort context:nil];
}
您的问题是您正在重新说明
拼图
数组。它是类上的一个ivar,但是由于在viewDidLoad
方法中有NSMutableArray*puzzles=…
,它将覆盖实例变量。如果您要NSLog(@“%@”,拼图)
在shuffle方法中,您将看到它记录(null)
简单的修复方法是删除viewDidLoad
方法中的NSMutableArray*
编辑
另外(正如Peter在评论中提到的)不要忘记保留数组。你的问题是你正在重新定义数组。它是类上的一个ivar,但是由于在
viewDidLoad
方法中有NSMutableArray*puzzles=…
,它将覆盖实例变量。如果您要NSLog(@“%@”,拼图)
在shuffle方法中,您将看到它记录(null)
简单的修复方法是删除viewDidLoad
方法中的NSMutableArray*
编辑
另外(正如Peter在评论中提到的)不要忘记保留数组。以下是我使用的:
- (void) shuffle
{
// Use the Fisher-Yates shuffle method (http://en.wikipedia.org/wiki/Fisher-Yates_shuffle):
/*
Random rng = new Random(); // i.e., java.util.Random.
int n = array.length; // The number of items left to shuffle (loop invariant).
while (n > 1)
{
int k = rng.nextInt(n); // 0 <= k < n.
n--; // n is now the last pertinent index;
int temp = array[n]; // swap array[n] with array[k] (does nothing if k == n).
array[n] = array[k];
array[k] = temp;
}
*/
NSUInteger n = [_cards count];
while(1 < n) {
NSUInteger k = random() % n;
n--;
[_cards exchangeObjectAtIndex:n withObjectAtIndex:k];
}
}
-(无效)洗牌
{
//使用Fisher-Yates shuffle方法(http://en.wikipedia.org/wiki/Fisher-Yates_shuffle):
/*
Random rng=new Random();//即java.util.Random。
int n=array.length;//要洗牌的项目数(循环不变)。
而(n>1)
{
int k=rng.nextInt(n);//0以下是我使用的:
- (void) shuffle
{
// Use the Fisher-Yates shuffle method (http://en.wikipedia.org/wiki/Fisher-Yates_shuffle):
/*
Random rng = new Random(); // i.e., java.util.Random.
int n = array.length; // The number of items left to shuffle (loop invariant).
while (n > 1)
{
int k = rng.nextInt(n); // 0 <= k < n.
n--; // n is now the last pertinent index;
int temp = array[n]; // swap array[n] with array[k] (does nothing if k == n).
array[n] = array[k];
array[k] = temp;
}
*/
NSUInteger n = [_cards count];
while(1 < n) {
NSUInteger k = random() % n;
n--;
[_cards exchangeObjectAtIndex:n withObjectAtIndex:k];
}
}
-(无效)洗牌
{
//使用Fisher-Yates shuffle方法(http://en.wikipedia.org/wiki/Fisher-Yates_shuffle):
/*
Random rng=new Random();//即java.util.Random。
int n=array.length;//要洗牌的项目数(循环不变)。
而(n>1)
{
int k=rng.nextInt(n);//0 noooooooo!!!!不要使用-sort
来实现洗牌。使用Fisher-Yates shuffle,它在O(n)而不是O(n log n)中运行,可能会生成更统一的结果(–Yates_shuffle)。更好的维基百科链接:steffen Myklebast:为什么不使用该问题的最高评分答案?这并不理想(int
通常在Cocoa中使用的类型是错误的),但这比基于未识别的排序进行洗牌要好。感谢大家的输入。以下是我得到想要的结果的结果。-(void)shuffle{nsuiger n=[puzzles count];而(1-sort
来实现洗牌。使用Fisher-Yates shuffle,它以O(n)而不是O(n log n)运行,并可能生成更统一的结果(–Yates_shuffle)更好的维基百科链接:steffen Myklebust:为什么不使用该问题的最高评分答案?这并不理想(int
在Cocoa中通常使用错误的类型),但它比基于未识别的排序进行洗牌要好。感谢大家的输入。以下是我最终得到的结果。-(无效)洗牌{NSUTEGER n=[puzzles count];而(1