Access to NSMutableArray member variable failed

I'm having problems with NSMutableArray. I am sure that I am doing something wrong with the NSMutableArray distribution, but it is not obvious that I am new to the iPhone. When I run the code below, I can add the MyObject object to the array_ objects and set a name, etc. NSLog displays the correct data.

But when I try to access the objects_ member from the printObject function, I get SIGABRT. Looks like the memory has been freed or something else?

Any help was appreciated.


@interface MyObject : NSObject {
    NSString *name;
}

-(void) SetName:(NSString*) name_str;
-(NSString*) GetName;

@end

@interface ObjectListViewController : UITableViewController {
    NSMutableArray* objects_;
}

-(void) initTableData;

@end

@implementation ObjectListViewController

- (void)initTableData {
    objects_ = [[NSMutableArray alloc] initWithCapacity:10];
    MyObject *obj = [MyObject alloc];
    [obj SetName:@"Test"];
    [objects_ addObject:obj];

    MyObject* testObj = (MyObject*)[objects_ objectAtIndex:0];
    NSLog([testObj GetName]);
}

- (void)printObject {
    MyObject* testObj = (MyObject*)[objects_ objectAtIndex:0];
    NSLog([testObj GetName]);
}

+3
source share
4 answers

init MyObject , . init NSObject self, . , :

, . init, NSObject, ; self.

Chuck , init , , MyObject.

, . , objects_ . , , , , .

, , printObject @interface. eykanal , printObject , .

, . _ initTableData , MyObject . , - , ?

, objectAtIndex? ? ?

+3
 MyObject *obj = [MyObject alloc];

:

 MyObject *obj = [[MyObject alloc] init];
+1
    @interface ObjectListViewController : UITableViewController {
        NSMutableArray* objects_;
    }

   @property (nonatomic, retain) NSMutableArray *objects_;
   -(void) initTableData;
   -(void) printObject;

    @end

@implementation ObjectListViewController
@synthesize objects_;
0

:

  • MyObj. NSObject NSObject , , self, , , .
  • . , "get" , , , NSData -getBytes: length:. -name -setName: . nitpick, , KVO KVC.
  • NSLog(someStringVariable) NSLog(@"%@", someStringVariable). , , . % @,% d,% s .., NSLog. - ​​ NSLog -initTableData​​li >
  • -AtIndex:

Having said all this, I do not see anything that could cause a specific problem. It is possible that the getter or setter for the name in MyObject is incorrect. Please post them.

0
source

Source: https://habr.com/ru/post/1763534/


All Articles