Reputation: 1447
I am trying to write my own class storing data about libraries and then store each object in an NSArray.
My interface:
#import <Foundation/Foundation.h>
@interface LibraryInfo : NSObject
{
NSString* name;
NSString* address;
NSString* link;
NSString* coordinates;
}
- (NSString*) getName;
- (NSString*) getAddress;
- (NSString*) getLink;
- (NSString*) getCoordinates;
- (void) setName: (NSString*)input;
- (void) setAddress: (NSString*)input;
- (void) setLink: (NSString*)input;
- (void) setCoordinates: (NSString*)input;
@end
My implementation:
#import "LibraryInfo.h"
@implementation LibraryInfo
- (void) setName: (NSString*)input
{
name = input;
}
- (void) setAddress: (NSString*)input
{
address = input;
}
- (void) setLink: (NSString*)input
{
link = input;
}
- (void) setCoordinates: (NSString*)input
{
coordinates = input;
}
- (NSString*) getName {
return name;
}
- (NSString*) getAddress {
return address;
}
- (NSString*) getLink {
return link;
}
- (NSString*) getCoordinates {
return coordinates;
}
@end
I write and store each object in the NSArray, like so:
LibraryInfo *library = [[LibraryInfo alloc] init];
[library setName:( ( name != nil && name.length > 0 ) ? name : @"No Name" )];
[library setAddress:( ( address != nil && address.length > 0 ) ? address : @"No Address" )];
[library setLink:( ( link != nil && link.length > 0 ) ? link : @"No Link" )];
[library setCoordinates:( ( coordinate != nil && coordinate.length > 0 ) ? coordinate : @"No Coordinates" )];
[libraries addObject: library];
Where 'libraries' is an NSArray.
Then I try to read the values like so:
cell.textLabel.text = [[ libraries objectAtIndex:indexPath.row ] getName ];
cell.detailTextLabel.text = [[ libraries objectAtIndex:indexPath.row ] getAddress ];
I think it may be something to do with the setting of the values, or maybe they are not properly stored or retained, but I get the following error and a marker by my 'setName' method
when the App 'crashes' and pauses:
self LibraryInfo *const 0x068a1760
input NSString * 0x068a3550
name NSString * 0x00000000
How do I fix this? I'm using Xcode 4.3 so no releases/retains needed.
Upvotes: 1
Views: 12367
Reputation: 18561
You need to declare properties. IVars aren't going to store your data the way you're expecting
LibraryInfo.h
@interface LibraryInfo : NSObject
@property (nonatomic, strong) NSString* name;
@property (nonatomic, strong) NSString* address;
@property (nonatomic, strong) NSString* link;
@property (nonatomic, strong) NSString* coordinates;
LibraryInfo.m
@implementation LibraryInfo
@synthesize name;
@synthesize address;
@synthesize link;
@synthesize coordinates;
Then creating libraries would be simple
LibraryInfo *library = [[LibraryInfo alloc] init];
library.name = (name.length > 0 ? name : @"No Name");
library.address = (address.length > 0 ? address : @"No Address")
library.link = (link.length > 0 ? link : @"No Link")
library.coordinates = (coordinates.length > 0 ? coordinates : @"No Coordinates")
Then in your TableView cell.
cell.textLabel.text = [libraries objectAtIndex:indexPath.row].name;
cell.detailTextLabel.text = [libraries objectAtIndex:indexPath.row].address;
Upvotes: 4