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 01-27-2012, 06:42 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default NSString ARRAY crashing..

hi there, I have a code
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    page = 0;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"text" ofType:@"txt"];
    
    NSError *error;
	NSString* content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
	allLines = [content componentsSeparatedByString: @"\n"];
	NSString *myText = [allLines objectAtIndex:page];
    
    
    textField.text = myText;
  
}

-(IBAction)next:(id)sender{
    page++;
    textField.text = [allLines objectAtIndex:page];

}
it is running ok but when click on the next button it crash.. the allLines NSArrazy is "global" defined in .h file.. so what is wrong..? thanx for advices...
vogueestylee is offline   Reply With Quote
Old 01-27-2012, 07:43 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vogueestylee View Post
hi there, I have a code
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    page = 0;
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"text" ofType:@"txt"];
    
    NSError *error;
	NSString* content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
	allLines = [content componentsSeparatedByString: @"\n"];
	NSString *myText = [allLines objectAtIndex:page];
    
    
    textField.text = myText;
  
}

-(IBAction)next:(id)sender{
    page++;
    textField.text = [allLines objectAtIndex:page];

}
it is running ok but when click on the next button it crash.. the allLines NSArrazy is "global" defined in .h file.. so what is wrong..? thanx for advices...
You need to read up on Cocoa memory management.

the method componentsSeparatedByString returns an autoreleased array. An autoreleased object is one that is temporary, and will go away if nobody retains it (or takes ownership of it.)

The cleanest way to fix your code is to change allLines to a retained property by adding this line to your header:

Code:
@property (nonatomic, retain) NSArray *allLines;
and adding

Code:
@synthesize allLines;
to your .m file.

Then, when you assign a value to allLines, use "property notation":



Code:
	self.allLines = [content componentsSeparatedByString: @"\n"];
By saying "self.allLines" you use the setter, which retains the object for you.

Then, in your dealloc method, add the line

Code:
self.allLines = nil;
That triggers the setter to release any old value stored in allLines before your object gets deallocated.
__________________
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 offline   Reply With Quote
Old 01-27-2012, 07:58 AM   #3 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default

Duncan C - your answer is simply PRO! thank you!

just for the future - how to get a number of lines in NSArray? some integer number how many lines of text are there..?

this is working for me: NSLog(@"%@", allLines);
but how to make a sum of objects?
vogueestylee is offline   Reply With Quote
Old 01-27-2012, 08:20 AM   #4 (permalink)
Registered Member
 
apatsufas's Avatar
 
Join Date: Jan 2011
Location: Thessaloniki, Greece
Posts: 121
apatsufas is on a distinguished road
Default

Take a look at the NSArray documentation here

You will see that NSArray has a method count which does what you want.
__________________
SQLed - Your Database Manager on the go

iAZConverter - Converts everything from A to Z
apatsufas is offline   Reply With Quote
Old 01-27-2012, 08:46 AM   #5 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 83
vogueestylee is on a distinguished road
Default

apatsufas - nice page, I got it, for others:

Code:
int mcount =[allLines count];
    
    NSLog (@"array = %i", mcount);
vogueestylee is offline   Reply With Quote
Old 01-27-2012, 09:34 AM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by vogueestylee View Post
apatsufas - nice page, I got it, for others:

Code:
int mcount =[allLines count];
    
    NSLog (@"array = %i", mcount);
Nice page?

That is the online version of the Xcode documentation. There is a full set of documentation that covers all this sort of thing built into Xcode.

Using the Xcode help system to search for documents on different APIs:

Pull down the help menu and select "Documentation and API reference"

Tap on the magnifying glass in the upper left corner, by the input box, and select "show find options" if it isn't active already.

Make the following change to the find options:
  • Under "Match type", select "contains". That lets you search for partial names. It's slower, but much more useful. You can type part of a method or class name and Xcode will find it.
  • Under doc sets, select the version of iOS you are using for development (iOS 5 perhaps) This narrows the search to the current SDK version, speeds up searching, and avoids duplicate search results.
  • Under Languages, select "C" and "Objective C". You need C because some of the Core Foundation frameworks are C. You want to turn off Java and JavaScript so your search results aren't cluttered with things you don't care about.

Now type in "NSArray" into the search field. The list of matches will be shown on the left side. You should see an entry under "Reference" for NSArray. Select it to open the NSArray class reference.
__________________
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 offline   Reply With Quote
Reply

Bookmarks

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: 406
10 members and 396 guests
Atatator, condor304, FrankWeller, imac74, MAMN84, mraalex, n00b, PowerGoofy, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

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