0

I'm not entirely sure if I wrote this array correct in the first place. Here is the .h in my app delegate.

NSString *text0;
...
NSString *text123;

NSMutableArray *fortunesArray;
}

@property(nonatomic,retain) NSMutableArray *fortunesArray;

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet AppViewController *viewController;

@end

Then in the app delegate.m I'm assigning all of them like such.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

text0 = @"Text here";
...
text123 = @"Text here";

self.fortunesArray = [NSMutableArray arrayWithObjects:text0,text1,text2,text3,text4,text5,text6,text7,text8,text9,text10,text11,text12,text13,text14,text15,text16,text17,text18,text19,text20,text21,text22,text23,text24,text25,text26,text27,text28,text29,text30,text31,text32,text33,text34,text35,text36,text37,text38,text39,text40,text41,text42,text43,text44,text45,text46,text47,text48,text49,text50,text51,text52,text53,text54,text55,text56,text57,text58,text59,text60,text61,text62,text63,text64,text65,text66,text67,text68,text69,text70,text71,text72,text73,text74,text75,text76,text77,text78,text79,text80,text81,text82,text83,text84,text85,text86,text87,text88,text89,text90,text91,text92,text93,text94,text95,text96,text97,text98,text99,text100,text101,text102,text103,text104,text105,text106,text107,text108,text109,text110,text111,text112,text113,text114,text115,text116,text117,text118,text119,text120,text121,text122,text123,nil];


 self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

I have tried this with NSArray and Mutable. The EXC_BAD_ACCESS is showing up pointing at text3 and before it was pointing at text5. If I cut out everything after about 50 the screen will open but when I finally try to have it work by clicking the button it resorts back to that bad access. (So can't tell if there is an issue with the views button yet because this issue is happening at this array repeatedly.) I'll post the code that calls it, but I'm pretty sure the main issue has something to do with this array.

In my view controller.m

-(IBAction)ganjaButton:(id)sender{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

int pressCount;
NSString *display;
if(pressCount%2==0){
    [sender setBackgroundImage:[UIImage imageNamed:@"nug2.png"] forState:UIControlStateNormal];
    display = [[NSString alloc] initWithFormat:@"%@",[appDelegate.fortunesArray objectAtIndex:40]];
}
else{
    [sender setBackgroundImage:[UIImage imageNamed:@"nug1.png"] forState:UIControlStateNormal];
    display = [[NSString alloc] initWithFormat:@"%@",[appDelegate.fortunesArray objectAtIndex:44]];
}
pressCount++;

label.text = display;
[display release];

}

Also yes in the above code the part that says AppDelegate is actually my AppDelagtes name.

Any help would be appreciated. Thanks.

steven
  • 698
  • 3
  • 8
  • 32
  • is `fortunesArray` synthesized or are you wrapping it with your own methods? Can you also point at which line you're getting the crash? (is it `self.fortunesArray = [NSMutableArray arrayWithObjects:text0..` ?) – alex-i Oct 05 '11 at 19:30
  • Try turning on NSZombies and check what causes the EXC_BAD_ACCESS - that way it will point you to the released object. The code looks fine I think, even with such a large number of coma separated values which I would hate to see in my code ;-). Enabling NSZombies: http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-on-xcode-4 – Mayjak Oct 05 '11 at 19:49
  • 1
    ganjaButton won't work right. You declare pressCount, never initialize it, test it, then increment it, then throw it away (when the method exits). Probably pressCount should be an instance variable. – Hot Licks Oct 05 '11 at 20:16

3 Answers3

4

Have you thought about storing all those text values in a plist and loading them into an array with [NSArray arrayWithContentsOfFile:filePath];?

That would be a MUCH cleaner solution in the first place.

Edit: To get the filePath, assuming your plist is named "textStrings.plist" you would use the following:

NSString *filePath = [bundle pathForResource:@"textStrings" ofType:@"plist"];
Christopher A
  • 2,961
  • 1
  • 22
  • 23
  • Ok that sounds much cleaner. So how would you write a plist with those strings? Do your write it like @"Text" or just Text? Also how would I got about calling a particular string of text from the plist. Lets say text45 for example or would they need to be stored into an array? – steven Oct 05 '11 at 19:33
  • There are two main types of property lists (plists): array based and dictionary based. An array based plist would be best for your needs here. His is an example to get you started: http://cl.ly/3V0m023u0e3G292k0k3k You will need to deserialize any plist into either a NSArray or a NSDictionary before you can access its contents. – Christopher A Oct 05 '11 at 20:11
1

Try this:

self.fortunesArray = [NSMutableArray arrayWithObjects:text0, text1, text2, nil];

I don't know if there's a maximum number of parameters that you can pass into a method, but if there is it's likely that you're exceeding that limit at 124, and probably also at 50. If everything works fine when you pass just a few objects into the array, you should just find a different way to create the array. Another answer mentions using a property list, which would be a fine solution. You could also use a plain old text file with some delimiter between strings, read that into a single string, and use NSString's -componentsSeparatedByString: method to create an array.

On the other hand, if you still have trouble with just a few objects in the array, you'll know that the problem lies elsewhere. I don't see any obvious problems, but I'd be on the lookout for other places in your code where the fortunesArray property is set.

Caleb
  • 124,013
  • 19
  • 183
  • 272
0

I think the array is getting autoreleased and thats why the crash is appearing. Try allocating memory for the array

self.fortunesArray =[[NSMutableArray alloc] initWithObjects:text0,text1...text123,nil];

and release the array once you are done with it. But I will Strongly recommend using plist file as suggested by Christopher.

Shri
  • 2,129
  • 2
  • 22
  • 32