Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 9    Views: 72

Red Dog
buzztouch Evangelist
Profile
Posts: 805
Reg: Jun 16, 2011
Southern Califo...
18,800
12/31/13 08:04 AM (11 years ago)

Trying to get string objects into NSArray

Hi Guys. I have an array using 9 static images. this is working: cards = [[NSArray alloc] initWithObjects: [UIImage imageNamed:@"Card0"], [UIImage imageNamed:@"Card1"], [UIImage imageNamed:@"Card2"], [UIImage imageNamed:@"Card3"], [UIImage imageNamed:@"Card4"], [UIImage imageNamed:@"Card5"], [UIImage imageNamed:@"Card6"], [UIImage imageNamed:@"Card7"], [UIImage imageNamed:@"CardBack"], nil]; I would like to give the console user control over what images to use, so my new code is: [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card0" defaultValue:@""]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card1" defaultValue:@""]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card2" defaultValue:@""]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card3" defaultValue:@""]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card4" defaultValue:@""]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card5" defaultValue:@""]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card6" defaultValue:@""]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card7" defaultValue:@""]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"CardBack" defaultValue:@""]; cards = [[NSArray alloc] initWithObjects: @"Card0", @"Card1", @"Card2" @"Card3" @"Card4" @"Card5" @"Card6" @"Card7" @"CardBack", nil]; Here is the error: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 8 beyond bounds [0 .. 2]' Any ideas why I am getting this? Or, am I going at this entirely wrong? Thank you.
 
Jake Chasan
Veteran developer
Profile
Posts: 1685
Reg: May 13, 2011
location unknow...
29,650
like
12/31/13 09:11 AM (11 years ago)
Some background: This error usually has to do with the enumeration (sorting through the elements of the array) has exceeded the array's size. Arrays increment from 0 to Length-1. You may have accidentally incremented farther than the array can go. A solution: You can resolve this error and have more streamlined code if you implement the following: 1. A for loop that goes from 0 to 8: for(int i = 0; i<cards.length; i++) 2. Within the for loop, you grab the JSON data: NSString *jsonString = [NSString stringWithFormat@"Card%i",i]; [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:jsonString defaultValue:@""]; So the entire code block would be: (I use a mutable array because it may be more efficient) NSMutableArray *cards = [[NSArray alloc] init]; for(int i = 0; i<cards.length; i++) { NSString *jsonString = [NSString stringWithFormat@"Card%i",i]; NSString* jsonValue = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:jsonString defaultValue:jsonString]; [cards addObject jsonValue]; } Does this help? Jake
 
Red Dog
buzztouch Evangelist
Profile
Posts: 805
Reg: Jun 16, 2011
Southern Califo...
18,800
like
12/31/13 09:48 AM (11 years ago)
Hi Jake. Property 'length' not found on object of type 'MSMutableArray'
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
12/31/13 10:33 AM (11 years ago)
it's count not length
 
Jake Chasan
Veteran developer
Profile
Posts: 1685
Reg: May 13, 2011
location unknow...
29,650
like
12/31/13 10:33 AM (11 years ago)
Oh, sorry, I was thinking of Java when I tried to access the length property. Try to use this: [cards count]; Also there is a typo in: NSMutableArray *cards = [[NSArray alloc] init];, it should be NSMutableArray *cards = [[NSMutableArray alloc] init]; Jake
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
12/31/13 10:55 AM (11 years ago)
I think your use of 'literals' might be messing with it. http://blog.bignerdranch.com/398-objective-c-literals-part-1/ **************************************************************** Are you trying to store the value from the bt string into the array. if so use something like this NSString *card0 = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card0" defaultValue:@""]; NSString *card1 = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card1" defaultValue:@""]; NSString *card2 = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card2" defaultValue:@""]; NSString *card3 = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card3" defaultValue:@""]; NSString *card4 = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card4" defaultValue:@""]; NSString *card5 = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card5" defaultValue:@""]; NSString *card6 = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card6" defaultValue:@""]; NSString *card7 = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card7" defaultValue:@""]; NSString *cardBack = [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"CardBack" defaultValue:@""]; cards = [[NSArray alloc] initWithObjects: card0, card1, card2, card3, card4, card5, card6, card7, cardBack, nil]; ************************************************************************ if all you are doing is storing the words card0 through 8 into the array then use something like this. NSArray *cards = @[ @"Card0", @"Card1", @"Card2", @"Card3", @"Card4", @"Card5", @"Card6, @"Card7", @"Card8"];
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
12/31/13 11:01 AM (11 years ago)
or like this: NSArray *cards = [[NSArray alloc] initWithObjects: [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card0" defaultValue:@""], [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card1" defaultValue:@""], [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card2" defaultValue:@""], [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card3" defaultValue:@""], [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card4" defaultValue:@""], [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card5" defaultValue:@""], [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card6" defaultValue:@""], [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"Card7" defaultValue:@""], [BT_strings getJsonPropertyValue:self.screenData.jsonVars nameOfProperty:@"CardBack" defaultValue:@""], nil]; (better than my previous post)
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
01/02/14 09:27 AM (11 years ago)
Did you get sorted out?
 
Red Dog
buzztouch Evangelist
Profile
Posts: 805
Reg: Jun 16, 2011
Southern Califo...
18,800
like
01/02/14 09:56 AM (11 years ago)
Hi Niraj. No luck. My way gives one error, Jakes gives another error, and Nads leads to another. Not a big deal. The plugin works fine as it stands, I was just trying to give the control panel user some more ways to customize it. They can still drop in any images they want before compiling, I thought it would be fun to change the app users experience after downloading from iTunes and eventually let the end user change their card pics with images from their camera roll. But that is way down the road. I'm not going to spend more time on it. I have other things that need my attention right now. Thanks for checking in and Happy 2014.
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
01/02/14 10:05 AM (11 years ago)
Good decision on priorities and where to expend your energies. We can sort it out in San Diego during the Buzzday! -- Niraj
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.