Hello I created a app on iOS 4.3.3 and than downloaded Xcode 4.2.1 and iOS sdk 5. It’s a navigation-based application and it has a mutable array for the tableview but it won’t create any strings for the array, so i added an object to array in the viewdidload but it is not working! So i added NSlog to the objects in my array and it says null! What should i do?
Here’s the viewdidload:
- (void)viewDidLoad
{
[super viewDidLoad];
self.soldc = [NSMutableArray arrayWithCapacity:0];
self.color = [NSMutableArray arrayWithCapacity:0];
appdelegate = (yard_sale_managerAppDelegate *)[[UIApplication sharedApplication] delegate];
appdelegate.object = self;
NSMutableArray *loadi = [[NSMutableArray alloc]initWithCapacity:0];
self.items = loadi;
self.sold = [NSMutableArray arrayWithCapacity:0];
self.prices = [NSMutableArray arrayWithCapacity:0];
alert = [[UIAlertView alloc] initWithTitle:@"Add product" message:@"Enter your product name and price." delegate:self cancelButtonTitle:@"Add" otherButtonTitles:@"Cancel", nil];
[alert addTextFieldWithValue:@"" label:@"name"];
[alert addTextFieldWithValue:@"" label:@"price"];
add = [alert textFieldAtIndex:0];
add.keyboardType = UIKeyboardTypeAlphabet;
add.keyboardAppearance = UIKeyboardAppearanceAlert;
add.autocapitalizationType = UITextAutocapitalizationTypeWords;
add.autocorrectionType = UITextAutocorrectionTypeNo;
add2 = [alert textFieldAtIndex:1];
add2.keyboardType = UIKeyboardTypeNumberPad;
add2.keyboardAppearance = UIKeyboardAppearanceAlert;
add2.autocapitalizationType = UITextAutocapitalizationTypeWords;
add2.autocorrectionType = UITextAutocorrectionTypeNo;
NSLog(@"%i", [color count]);
alert.tag=1;
self.resa = [[UIAlertView alloc] initWithTitle:@"Sale results" message:@"temple" delegate:self cancelButtonTitle:@"Close" otherButtonTitles: nil];
self.items = [NSMutableArray arrayWithContentsOfFile:[self itemsp]];
self.prices = [NSMutableArray arrayWithContentsOfFile:[self pricesp]];
self.sold = [NSMutableArray arrayWithContentsOfFile:[self soldp]];
self.soldc = [NSMutableArray arrayWithContentsOfFile:[self soldcp]];
self.color = [NSMutableArray arrayWithContentsOfFile:[self colorsp]];
if ([self.soldc count]==0) {
}
//self.soldc = [NSMutableArray arrayWithContentsOfFile:[self soldcp]];
self.color = [NSMutableArray arrayWithContentsOfFile:[self colorsp]];
[loadi release];
self.resa.tag = 2;
[self.items addObject:@"chair"];
[self.prices addObject:@"30"];
[self.color addObject:@"0"];
NSLog(@"%@",self.items);
}
If you need more information tell me!
This is the log:
2012-01-08 21:31:13.409 yard sale manager[1152:f803] 0
2012-01-08 21:31:13.411 yard sale manager[1152:f803] (null)
edit: read this its important!!!!!
a lot of people didn’t notice that piece of code:
[self.items addObject:@”chair”];
[self.prices addObject:@”30″];
[self.color addObject:@”0″];
NSLog(@”%@”,self.items);
This line
instantiates and assigns an empty array, you then do not add anything to it therefore the
0count.Then this line
is probably the cause. Does
[self itemsp]return a valid file path that points to a file that can be parsed into anNSArray.There are also a lot of other issues with this code.
Update
This line
must be returning
nil.Confirm by adding this line straight after it
How can I make this assumption
The reason I am making this assumption is because you are getting
(null). When you calladdObject:onnilyou will getnilreturned and it is a no-op. As you can see from this snippetJust because
myArrayholds a pointer to anNSMutableArrayit does not mean that you actually have one.Therefore the last time you assign to
self.itemsis in:which means
[NSMutableArray arrayWithContentsOfFile:[self itemsp]]is returningnil