-2

I would like to use a NSArray like property in whole class (self.array), but I don't know how. Now I have this code in my LoginVC:

LoginVC.h:

#import <UIKit/UIKit.h>

@interface LoginVC : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UITextField *passwordTextField;
@property (strong, nonatomic) IBOutlet UIScrollView *loginButton;
@property (nonatomic, strong) NSArray *allUsers;

@end

LoginVC.m:

- (void)viewDidLoad
{
    if (debug == 1) {
        NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));
    }
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UserController *userController = [[UserController alloc]init];

    [userController getUsers];

    self.allUsers = [NSArray new];

    self.allUsers = [userController getUsers];

}

The method getUser return a NSArray, but this solution doesnt work. The llbd report an error: *Thread 1: EXC_BAD_ACCESS (code=2, address=0x38)* I think the problem is with initialization the array and assignment.

Declaration of getUsers:

-(NSArray *)getUsers{
    CoreDataHelper *cdh = [(AppDelegate *)[[UIApplication sharedApplication]delegate] cdh];

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"User"];

    NSArray *users = [cdh.context executeFetchRequest:request error:nil];

    return users;

}

The method "getUsers" works correctly.

Here is a screen of the error. Its strenge, because self.allUsers contents one object with userName "admin" and its absolutely correct.

https://www.dropbox.com/s/ui5rtq9ov0p2xx9/Screenshot%202014-03-02%2019.24.53.png

I think the problem is in array, because when I delete the code with assignment to array. The error disappears.

krata
  • 101
  • 10
  • Post the `getUsers` method, or at least the relevant parts. – AMayes Mar 02 '14 at 17:59
  • This kind of stuff is covered in any introduction to Objective-C and iOS programming. – dandan78 Mar 02 '14 at 18:00
  • Hey, I recommend reading this: http://loufranco.com/blog/understanding-exc_bad_access – Sascha Manuel Hameister Mar 02 '14 at 18:00
  • Your property declaration is ok. – Avt Mar 02 '14 at 18:01
  • `self.allUsers = [NSArray new];` is pointless since you immediately overwrite that array with a different one. But that wouldn't cause this problem - can you post your whole stack trace? Why do you think this error is related to this array? – Aaron Brager Mar 02 '14 at 18:07
  • Here is screen of stack trace etc. It's strange, because self.allUsers content one object with userName admin and Its absolutely correct, but the application die. https://www.dropbox.com/s/ui5rtq9ov0p2xx9/Screenshot%202014-03-02%2019.24.53.png – krata Mar 02 '14 at 18:27

1 Answers1

0

The key here is you UserController object. Your log output says failed to call designated initialiser for NSManageObject Subclass UserController which makes me assume it is an NSManagedObject.

NSManagedObjects are autoreleased and in your case, it is not retained by an NSManagedObjectContext, which makes it cause the EXC_BAD_ACCESS. Try to run your app with zombies enabled, it should show you what object is being released, and causing the error.

EDIT: It may be that I'm wrong, but it could also be that the way your CoreDataHelper is initialised is relevant. See this answer for how to enable Zombies when you test your app: How to enable NSZombie in Xcode?

Community
  • 1
  • 1
Audun Kjelstrup
  • 1,430
  • 8
  • 13
  • Thanks. I enable the Zombie mode (Product->Edit Scheme->Diagnostics-> Click Enable Zombie Objects), but nothing has changed (no new error). But I solved the problem as follows: The class UserController object inherited from class User and class User inherited from class NSManaged object. I changed it and now class UserController inherits from NSObject and it works... But I dont know if its conceptually correct. – krata Mar 02 '14 at 22:07
  • Have a look at Paul Goracke's slides about Core Data: http://xcoders.s3.amazonaws.com/2014-02-13%20Core%20Data%20Potpourri.pdf IMHO, he is spot on in how the code should be organised. Conceptually, unless the object is supposed to be stored in a context, it should inherit from NSObject, not NSManagedObject. – Audun Kjelstrup Mar 03 '14 at 07:07