-4

I try to get info for the array in the console but the NSLOg does not show anything. This is a class where i store data for the app. Here is the code.

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

@interface DataModel : NSObject

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

@end

and in the .m file

#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

Where is the problem? I can't figure it out.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
Newbie
  • 25
  • 7
  • Where is the `_array` method called? Why is it called `_array`? Are you attempting to override the default getter method of the `array` property? – trojanfoe Nov 13 '14 at 11:01
  • You have two NSLog statements here. Both of them are inside methods that are never called. Are you calling the method _array or _array2 anywhere? Also, methods should begin with a letter not an underscore. – Fogmeister Nov 13 '14 at 11:01
  • I never called these methods. I just want to check if the content of the arrays is wrong. How to do that? – Newbie Nov 13 '14 at 11:04
  • 4
    So you want to know why `NSLog()` doesn't show anything when it's never called? Could you be a little more focused please? What is the issue? – trojanfoe Nov 13 '14 at 11:05

1 Answers1

0

You need to access from anywhere to that getters, after that you will see logs. For example try to do that in your AppDelegate.m

#import "DataModel.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    DataModel *data = [DataModel new];
    data.array;
    data.array2;
}
l0gg3r
  • 8,864
  • 3
  • 26
  • 46
  • One more question. In app delegate I have a warning “Property access results unused - getters should not be used for side effects” whats the issue? – Newbie Nov 13 '14 at 11:16
  • yes, that's normal. that's because we are not using return value, you can port for loop to appdelegate, where you're logging the content of array – l0gg3r Nov 13 '14 at 11:29