NSLOG不起作用

时间:2014-11-13 10:57:54

标签: objective-c xcode nsobject nslog

我尝试在控制台中获取数组的信息,但NSLOg没有显示任何内容。这是一个我为应用程序存储数据的类。这是代码。

#import <Foundation/Foundation.h>
#import "DAObject.h"

@interface DataModel : NSObject

@property (strong, nonatomic) NSArray *array;
@property (strong, nonatomic) NSArray *array2;

@end

并在.m文件中

#import "DataModel.h"
@implementation DataModel

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {               
    }
    return self;
}

- (NSArray *) _array
{
    DAObject *obj1 = [[DACityObject alloc] init];
    obj1.name = @"Obj1";

    DACityObject *obj2 = [[DACityObject alloc] init];
    obj2.name = @"Obj2";

    _array = @[ obj1, obj2];

    for (int i = 0; i < [_array count]; i++)
    {
        NSLog(@"Item %d = %@", i, [_array objectAtIndex:i]);
    }

    return _array;
}

- (NSArray *) array2 {    
    _array2 = [[NSArray alloc] initWithObjects:@"icon1.png", @"icon2.png",@"icon3.png",   nil];

    for (int i = 0; i < [_array2 count]; i++) {
        NSLog(@"Item %d = %@", i, [_array2 objectAtIndex:i]);
    }
    return _array2;
}

@end

问题出在哪里?我无法弄清楚。

1 个答案:

答案 0 :(得分:0)

您需要从任何地方访问该getter,之后您将看到日志。 例如,尝试在AppDelegate.m中执行此操作

#import "DataModel.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    DataModel *data = [DataModel new];
    data.array;
    data.array2;
}