2012-07-09 69 views
0

我知道这是一个更好的解决方案,使用arc4random(它在我的试用函数列表上),但我想先尝试使用rand()stand(time(NULL))函数。我创建了一个NSMutableArray并用5个数据查看它。测试它有多少个数字是好的。但是,当我试图用按钮功能加载对象还给我object <sampleData: 0x9a2f0e0>随机发生器从数组中获取数据不显示

- (IBAction)generateNumber:(id)sender { 

     srand(time(NULL)); 
    NSInteger number =rand()% ds.count ; 
    label.text = [NSString stringWithFormat:@"object %@", [ds objectAtIndex:number] ]; 
    NSLog(@"%@",label.text); 

} 

虽然我觉得主要原因是方法本身,我已经粘贴下面只是柜面我所做的代码的其余部分任何错误的地方。

ViewController.h

#import <UIKit/UIKit.h> 
#import "sampleData.h" 
#import "sampleDataDAO.h" 
@interface ViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UILabel *label; 
@property (weak, nonatomic) IBOutlet UIButton *onHitMePressed; 
- (IBAction)generateNumber:(id)sender; 
@property(nonatomic, strong) sampleDataDAO *daoDS; 
@property(nonatomic, strong) NSMutableArray *ds; 
@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize label; 
@synthesize onHitMePressed; 
@synthesize daoDS,ds; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    daoDS = [[sampleDataDAO alloc] init]; 
    self.ds = daoDS.PopulateDataSource; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [self setLabel:nil]; 
    [self setOnHitMePressed:nil]; 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (IBAction)generateNumber:(id)sender { 

    srand(time(NULL)); 
NSInteger number =rand()% ds.count ; 
label.text = [NSString stringWithFormat:@"object %@", [ds objectAtIndex:number] ]; 
NSLog(@"%@",label.text); 

} @end

sampleData.h

#import <Foundation/Foundation.h> 

@interface sampleData : NSObject 
@property (strong,nonatomic) NSString * object; 
@end 

sampleData.m

#import "sampleData.h" 

@implementation sampleData 
@synthesize object; 
@end 

sampleDataDAO.h

#import <Foundation/Foundation.h> 
#import "sampleData.h" 
@interface sampleDataDAO : NSObject 
@property(strong,nonatomic)NSMutableArray*someDataArray; 

-(NSMutableArray *)PopulateDataSource; 
@end 

sampleDataDAO.m

#import "sampleDataDAO.h" 

@implementation sampleDataDAO 
@synthesize someDataArray; 

-(NSMutableArray *)PopulateDataSource 
{ 
    someDataArray = [[NSMutableArray alloc]init]; 

    sampleData * myData = [[sampleData alloc]init]; 
    myData.object= @"object 1"; 
    [someDataArray addObject:myData]; 
    myData=nil; 



    myData = [[sampleData alloc] init]; 
    myData.object= @"object 2"; 
    [someDataArray addObject:myData]; 
    myData=nil; 

    myData = [[sampleData alloc] init]; 
    myData.object= @"object 3"; 
    [someDataArray addObject:myData]; 
    myData=nil; 

    myData = [[sampleData alloc] init]; 
    myData.object= @"object 4"; 
    [someDataArray addObject:myData]; 
    myData=nil; 

    myData = [[sampleData alloc] init]; 
    myData.object= @"object 5"; 
    [someDataArray addObject:myData]; 
    myData=nil; 



    return someDataArray; 
} 


@end 

回答

1

什么我猜是怎么回事是NSLog的功能,不能打印样本数据类内的数据,因为它不是一个标准类。不是标准类必须实现“描述”方法。当你打印出你的班级时,你得到的是指向它的指针,因为nslog无法知道如何打印出班级中的数据。

如果你想在该标签/ nslog上打印的是你的类“sampledata”中的nsstring,你应该访问该属性。

这可以通过以下方式进行:

SampleData *instanceOfSampleData = (SampleData*)[ds objectAtIndex:number]; 

label.text = [NSString stringWithFormat:@"object %@", instanceOfSampleData.object]; 
+0

哇感谢!完全是我想要的方式。感谢您提供关于标准课程和非标准课程的启发。 – 2012-07-09 09:22:52