0

arc4random を使用したより良い解決策があることは知っていますが (それは私の試してみる関数のリストにあります)、最初にrand()andstand(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);

} @終わり

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
4

1 に答える 1

1

私が推測していることは、nslog関数は、標準クラスではないため、サンプルデータクラス内のデータを出力できないということです。標準クラスではなく、「description」メソッドを実装する必要があります。nslogにはクラス内のデータを印刷する方法を知る方法がないため、クラスを印刷するときに得られるのは、クラスへのポインターです。

そのlabel/nslogに出力するのが、クラス「sampledata」内のnsstringである場合は、プロパティにアクセスする必要があります。

これは、次の方法で実行できます。

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

label.text = [NSString stringWithFormat:@"object %@", instanceOfSampleData.object];
于 2012-07-09T09:12:17.190 に答える