0

I have built up an array of objects, created from a class, I wrote before. I just wanted to access some of the class variables while looping through the array:

- (void)viewDidLoad
{
       [super viewDidLoad];

       NSMutableArray *medicalCenters = [(AppDelegate *)[[UIApplication sharedApplication] delegate] medicalCenters];

       for (MedicalCenter *row in medicalCenters){
           NSString *latitude = row.MCLatitude;
           NSLog(@"%@", latitude);
       }
}

the Class File I've created look like this:

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MedicalCenter : NSObject
{
    CLLocationCoordinate2D _coordinate;
}

@property (nonatomic, strong) NSString *medicalCenter;
@property (nonatomic, assign) NSString *MCLatitude;
@property (nonatomic, assign) NSString *MCLongitude;
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;

- (id)initWithName:(NSString *)medicalCenter coordinate:(CLLocationCoordinate2D)coordinate;

@end

Whenever I built up the NSString *latitude = row.MCLatitude, I got the message: Bad_excess Code:1

But when I just list the objects in the array medicalCenters, I can see them all............ Did I miss something important?

Thanks Sebastian

1
  • 1
    Why did you use assign for MCLatitude and MCLongitude? Commented Jun 16, 2012 at 17:18

1 Answer 1

1

That's because of bad memory management, MCLatitude and MCLongitude property are set with (assign), they're not being retained properly, don't know the reason you set them up as assign, no reason that I can see, so if you change them to be strong, and use setter to assign the value, it should be fine.

@property (nonatomic, strong) NSString *MCLatitude;
@property (nonatomic, strong) NSString *MCLongitude;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you guys... Before I hat MCLatitude as string var, I declared it as int. And therefore had the assign property. I just changed the code and now it works perfectly fine!!! Thank you all
You should typically use copy instead of strong for NSString. You don't want your string value unexpectedly changing on you if another class also has a pointer to the same string and makes a change. Rule of thumb is that for any class that has a mutable counterpart i.e. NSMutableString, NSMutableArray, you should always copy them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.