gpt4 book ai didi

objective-c - 随机生成器从不显示的数组中获取数据

转载 作者:行者123 更新时间:2023-11-29 04:33:23 25 4
gpt4 key购买 nike

我知道使用 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);

} @结束

样本数据.h

#import <Foundation/Foundation.h>

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

样本数据.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

最佳答案

我猜发生的事情是 nslog 函数无法打印示例数据类中的数据,因为它不是标准类。非标准类必须实现“描述”方法。当你打印出你的类时,你得到的是指向它的指针,因为 nslog 无法知道如何打印出你的类中的数据。

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

这可以通过以下方式完成:

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

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

关于objective-c - 随机生成器从不显示的数组中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11391600/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com