Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-25-2011, 08:44 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Location: Vancouver Island, Canada
Posts: 22
jemicha is on a distinguished road
Default Array of arrays? both static

What is the best way to create and then search an array within an array made up of date from a CSV table?

So far I have:

Code:
 NSArray *containerArray = [dataString componentsSeparatedByString:@"\n"]; //read in data from CSV file and separate it into 'row' strings
    NSArray *rowTemp; //local variable just for my sake
    NSMutableArray *tableArray;//mutable array to hold the row arrays
    
    //For each index of containerArray:
    //take the string object (string of CSV data) and then,
    //create an array of strings to be added into the final tableArray
    
        for (int i = 0; i < [containerArray count]; i++) {
        rowTemp = [[containerArray objectAtIndex:i] componentsSeparatedByString:@","];
        [tableArray addObject:rowTemp];        
        }
Then when I try the following, it returns: (null)

Code:
NSLog(@"fRow 6, cell 1: %@", [[tableArray objectAtIndex:5] objectAtIndex:0]);
any ideas? is there a better way?

Thanks in advance.

Last edited by jemicha; 12-25-2011 at 09:02 PM.
jemicha is offline   Reply With Quote
Old 12-25-2011, 08:53 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jemicha View Post
What is the best way to create and then search an array within an array made up of date from a CSV table?

So far I have:



Then when I try the following, it returns: (null)



any ideas? is there a better way?

Thanks in advance.

Use code tags, not quotes to include code.

In this code:

Code:
NSArray *containerArray = [dataString componentsSeparatedByString:@"\n"]; //read in data from CSV file and separate it into 'row' strings
NSArray *rowTemp; //local variable just for my sake
NSMutableArray *tableArray;//mutable array to hold the row arrays

//For each index of containerArray:
//take the string object (string of CSV data) and then,
//create an array of strings to be added into the final tableArray

for (int i = 0; i < [containerArray count]; i++) {
rowTemp = [[containerArray objectAtIndex:i] componentsSeparatedByString:@","];
[tableArray addObject:rowTemp]; 
}
You define a local variable tableArray, but then never create an array object. In non-ARC code, this should crash when you try to add an object to tableArray, since it will contain an invalid memory address.

In ARC, stack variables get set to nil for you, so the call to


[tableArray addObject:rowTemp];


Will simply not do anything, since tableArray will be nil.

You should change the declaration of your tableArray to read like this:

Code:
NSMutableArray *tableArray = [NSMutableArray arrayWithCapacity: [containerArray count]];
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-25-2011, 09:09 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2011
Location: Vancouver Island, Canada
Posts: 22
jemicha is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post

You define a local variable tableArray, but then never create an array object. In non-ARC code, this should crash when you try to add an object to tableArray, since it will contain an invalid memory address.

In ARC, stack variables get set to nil for you, so the call to


[tableArray addObject:rowTemp];


Will simply not do anything, since tableArray will be nil.

You should change the declaration of your tableArray to read like this:

Code:
NSMutableArray *tableArray = [NSMutableArray arrayWithCapacity: [containerArray count]];
That did indeed work, thanks for the prompt reply, and on Christmas!

I am at the very early stages of learning this stuff (started a month ago) and there are definitely some gaps I need to fill.

Just tried with a more familiar to me version:
Code:
NSMutableArray *tableArray = [[NSMutableArray alloc] initWithCapacity:[containerArray count]]
and I completely understand why now, thanks so much for the help!
jemicha is offline   Reply With Quote
Old 12-25-2011, 09:19 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2011
Location: Vancouver Island, Canada
Posts: 22
jemicha is on a distinguished road
Default

Thanks again for your prompt and helpful answer Duncan C. I saw a post by you from the summer saying that you did not like to use arrays of arrays for this type of application.

Would you have gone about this differently?

Your previous comment.[/url] http://www.iphonedevsdk.com/forum/ip...tml#post342434
jemicha is offline   Reply With Quote
Old 12-26-2011, 10:46 AM   #5 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jemicha View Post
That did indeed work, thanks for the prompt reply, and on Christmas!

I am at the very early stages of learning this stuff (started a month ago) and there are definitely some gaps I need to fill.

Just tried with a more familiar to me version:
Code:
NSMutableArray *tableArray = [[NSMutableArray alloc] initWithCapacity:[containerArray count]]
and I completely understand why now, thanks so much for the help!

The alloc/init form creates a retained object. You will be responsible for releasing it when you are done with it.

The form I used creates an autoreleased object. It will get released automatically if you don't retain it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-26-2011, 10:59 AM   #6 (permalink)
Registered Member
 
Join Date: Dec 2011
Location: Vancouver Island, Canada
Posts: 22
jemicha is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
The alloc/init form creates a retained object. You will be responsible for releasing it when you are done with it.

The form I used creates an autoreleased object. It will get released automatically if you don't retain it.
Well that would be much nicer, thanks for the explanation.

Am I to understand that alloc/init will be retained even when using ARC?
jemicha is offline   Reply With Quote
Old 12-26-2011, 11:33 AM   #7 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,005
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jemicha View Post
Well that would be much nicer, thanks for the explanation.

Am I to understand that alloc/init will be retained even when using ARC?
ARC hides most of that from you. What matters in ARC is the variable you assign it to. Local variables are strong by default, so they retain objects saved to them as long as they are in scope. When a local variable goes out of scope, the object it contained gets released.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is online now   Reply With Quote
Old 12-26-2011, 11:58 AM   #8 (permalink)
Registered Member
 
Join Date: Dec 2011
Location: Vancouver Island, Canada
Posts: 22
jemicha is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
ARC hides most of that from you. What matters in ARC is the variable you assign it to. Local variables are strong by default, so they retain objects saved to them as long as they are in scope. When a local variable goes out of scope, the object it contained gets released.
Makes sense, thanks for the explanation.

The core functionality of my very first 'real' app is up and running thanks to your help, Duncan C.

Now on to the details...
jemicha is offline   Reply With Quote
Reply

Bookmarks

Tags
array, array problem

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
13 members and 377 guests
7twenty7, AppsBlogger, Creativ, Dalia, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:51 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0