views:

123

answers:

5

Hi, I declared

NSString    *dayinfield[43];

and fill it in

-(void)DrawDemo {
dayinfield[2] = @"hallo";
dayinfield[3] = @"test";

// also i can read it
NSLog (@"show: %@",dayinfield[2]);
//works fine
}

but when i like to read its content in another function (same class)

-(void)ReadData
{
NSLog (@"show: %@",dayinfield[2]);
// I get random infos or “EXC_BAD_ACCESS
}

How do I initialize the NSString Array correct so I can reach its content in each of my functions??

Thanks chris

A: 

This would happen if you never initialized the array (or the parts of it you are accessing) - if you haven't called -DrawDemo before -ReadData or used different indices than the ones posted here, the array would simply contain garbage values.

Try to initialize the array contents to nil or @"" in your initializer method and see if the problem persists.

Alternatively consider using a suitable Cocoa container.

Georg Fritzsche
For sure DrawDemo is before ReadData, as mentioned IN DrawDemo I can read the Data perfect, just outside in another function its just deallocated...?? Any short Demo even something else welcomed> I just need a array of nsstrings that I can fill and read from wherever in my class :)
christian Muller
@christian: There, quick and dirty: http://pastebin.com/jyYaVNxm
Georg Fritzsche
By the way, the fact that you can read it in `-DrawDemo` but not in `-ReadData` is exactly why i think `-DrawDemo` might not be called first.
Georg Fritzsche
+1  A: 

If you only assign literals to the array elements, this should not be a problem. But if you use other strings, you have to retain the instances manually when using a C array.

By the way: Objective-C methods start with a lowercase letter.

Nikolai Ruhe
thx remembering about the lowercase.. even did it now :) but my string array prob still exist. Any short Demo even something else welcomed> I just need a array of nsstrings that I can fill and read from wherever in my class :)
christian Muller
A: 

It's memory is probably being released before your second call. Assuming you have declared dayinfield as an ivar (and the fact that you don't get bad access all the time) your string aren't properly retained.

Initialise the strings like this:

dayinfield[2] = [[NSString alloc] initWithString:@"hallo"];
dayinfield[3] = [[NSString alloc] initWithString:@"test"];

and you should release them after you're class is being deallocated (See Memory Management Guide).

Also, obviously it depends on what you want to do, but it might be easier if you use NSArray instead of C arrays.

Mo
@Mo: `var = [@"hello" retain];` is perfectly valid, too.
Nikolai Ruhe
String literals don't get deallocated under you (see [here](http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/CreatingStrings.html)): *"The compiler makes such object constants unique on a per-module basis, and they’re never deallocated, though you can retain and release them as you do any other object."*.
Georg Fritzsche
+1 on some random answer of yours, since I cannot give credit to this comment!
Nikolai Ruhe
For sure DrawDemo is before ReadData, as mentioned IN DrawDemo I can read the Data perfect, just outside in another function its just deallocated...?? Any short Demo even something else welcomed> I just need a array of nsstrings that I can fill and read from wherever in my class :)
christian Muller
I tried also this now in my viewDidLoadJust to be sure its initialised. for (int i = 0; i < 42; i++) { dayinfield[i] = [[NSString alloc] initWithString:@" "]; }---but still the same... i can read it in DrawDemo, but in ReadData its already gone :(
christian Muller
@Christian: As per the comment above, there is no need to alloc a new string. Those string literal objects ignore release and retain messages.
Georg Fritzsche
A: 

Please change your code to the following

In your .H file

NSArray* daysInField[50]; NSString* data;

In your viewDidLoad you can initialize all 50 members

-(void)DrawDemo {
data = @"halo";
dayinfield[2] = data;
dayinfield[3] = @"test";

// also i can read it 
NSLog (@"show: %@",dayinfield[2]);
//works fine
}


-(void)ReadData
{
 NSLog (@"show: %@",[dayinfield objectAtIndex:2]);
// this should work
}
Cocoa Dev
I cannot see any sense in this. Sounds like fishing in the dark.
Nikolai Ruhe
i feel same... fishing in the dark. I made a demo app now with just a few content and it works. Will build my main app now around this demo and see why there the error appears. will keep you updated. Because funny.. i tried also with nsmutablearray.. and had the same problem?!
christian Muller
A: 

What you have in the OP should work although it is an exercise in sheer masochism to use old school C arrays with objects.

I ran this code:

@interface TestClass : NSObject {
    NSString *a[1];
}
- (void) drawDemo;
- (void) readData;
@end 

@implementation TestClass

- (void) drawDemo{
    a[0]=@"A Zero";
    a[1]=@"A One";
}//------------------------------------- (void) drawDemo------------------------------------

- (void) readData{
    NSLog(@"a[0]=%@,a[1]=%@",a[0],a[1]);
}//------------------------------------- (void) readData------------------------------------

@end

TestClass *tc=[[TestClass alloc] init];
[tc drawDemo];
[tc readData];

... and got this output:

a[0]=A Zero,a[1]=A One

Your problem is elsewhere in your code. There is no compelling reason to use C arrays with objects. You gain nothing and you have to watch them like a hawk to prevent errors.

TechZen