I know this has been asked before, but there is no answer that I have found useful.
First off here is my code
// load the .csv file with all information about the track
NSError *error;
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"csv" inDirectory:nil];
NSString *datastring1 = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
NSArray *datarow = [datastring1 componentsSeparatedByString:#"\r"];
//fill arrays with the values from .csv file
NSArray *data_seg = [datarow objectAtIndex:0]; //segment number
NSArray *data_slength = [datarow objectAtIndex:1]; //strait length
NSArray *data_slope = [datarow objectAtIndex:2]; //slope
NSArray *data_cradius = [datarow objectAtIndex:3]; //circle radius
NSArray *data_cangle = [datarow objectAtIndex:4]; //circle angle
NSLog(#"%i", [data_seg count]);
Okay, so there is the code, and I read that is has something to do with autorelease, but I was not able to add a retain like NSArray *data_seg = [[datarow objectAtIndex:0] retain]
When I run the code, I get [__NSCFString count]: unrecognized selector sent to instance 0x9d1ad50
Any help is appreciated, I'm not good at programming, and I am very new.
componentsSeparatedByString method returns an NSArray of NSString. Every item that you extract from datarow array is an NSString and an NSString doesn't respond to 'count'. Your code starting at //fill arrays is incorrect. Every objectAtIndex call will return an NSString*.
This is another way of saying that the datatype for data_seg is NSString* (not NSArray*).
With the corrected code snippet, the problem is because data_seg is a string, and -count is not a method of NSString. It seems you think data_seg is an NSArray.
Look at the documentation for -[NSString componentsSeparatedByString:] and see what it returns -- strings! So you get back an array of strings. So what you want is:
NSString *data_seg = [datarow objectAtIndex:0]; //segment number
NSLog(#"my segment number is: %#", data_seg);
Related
My code is as follows:
NSArray *modifyVersionOnDevice [FileHandler parseFile:devicepath];
NSString *param = [modifyVersionOnDevice objectAtIndex:1];
//param at this point is one element with string of "#MAJREV: 3"
//As soon as I run the next line, I get an error Unrecognized selector sent
to instance.
NSArray *d =[param componentsSeparatedByString:#":"];
If I hard code
NSString *param =#"#MAJREV: 3"; it works
Turns out you have to convert param into a literal string.
NSString *param = [NSString stringWithFormat:#"%#",[modifyVersionOnDevice objectAtIndex:1]];
Then
NSArray *d =[param componentsSeparatedByString:#":"];
Can you please tell me the file string entered in the array, and then you have to change an element in this array. doing so:
NSMutableArray *user;
...
NSString* filePath1 = #"user";
NSString* fileRoot1 = [[NSBundle mainBundle] pathForResource:filePath1 ofType:#"txt"];
NSString* fileContents1 =[NSMutableString stringWithContentsOfFile:fileRoot1 encoding:NSUTF8StringEncoding error:nil];
user = [fileContents1 componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
money= [user[0] intValue]-2;
user[0]=[NSString stringWithFormat:#"%d",money];
swears
- [__NSArrayI ReplaceObjectAtIndex: withObject:]: unrecognized selector sent to instance 0x12fd4270
The componentsSeparatedByCharactersInSet: return a NSArray even if you local variable is a mutable array the value returned is not. So you will need to create a mutable copy.
Just can easily do this by call the mutableCopy on NSArray:
user = [[fileContents1 componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet] mutableCopy];
I have an NSArray, where each object contains a specific class called Card. Card has a description method. I want to join all objects in the array using the output of the description method, separated by spaces. Is there a simple to do this, without manually iterating the NSArray and manipulating NSString?
Something akin to the following made-up code?
NSArray *myArray = getCards(); // fetches 10 items or more
NSString *myString = [myArray joinUsingDescriptionMethodSeparatedBy:#" "];
or
NSString *myString = [NSString stringFromArrayDescriptionMethods:myArray separatedBy:#" "];
Naturally ,I could implement this myself but I suspect there could be something already present that does this.
I don't think that there is such a method. You can also implement it in a Category for NSString.
Sorry, I found this:
NSString * result = [[array valueForKey:#"description"] componentsJoinedByString:#""];
From the documentation:
Constructs and returns an NSString object that is the result of
interposing a given separator between the elements of the array.
- (NSString *)componentsJoinedByString:(NSString *)separator
Do this for description method of each NSArray item:
NSMutableString * result = [[NSMutableString alloc] init];
for (NSObject * obj in array)
{
[result appendString:[NSString stringWithFormat:#" %#"[obj description]]];
}
NSLog(#"The concatenated string is %#", result);
I am having trouble with compatibility between NSArray and NSMutableArray?
--> Incompatible Objective-C types assigning "struct NSArray *", expected "struct NSMutableArray"
NSMutableArray *nsmarrRow;
NSString *nsstrFilePath = [[NSBundle mainBundle] pathForResource: nsstrFilename ofType: nsstrExtension];
NSString *nsstrFileContents = [NSString stringWithContentsOfFile: nsstrFilePath encoding:NSUTF8StringEncoding error: NULL];
//break up file into rows
nsmarrRow = [nsstrFileContents componentsSeparatedByString: nsstrRowParse];//<--Incompatible Objective-C types assigning "struct NSArray *", expected "struct NSMutableArray"
I have tried making the "NSString declaration" to "NSMutableString"... made more problems.
thanks
You can't get a mutable array by doing
nsmarrRow = [nsstrFileContents componentsSeparatedByString: nsstrRowParse];
You will need to,
nsmarrRow = [NSMutableArray arrayWithArray:[nsstrFileContents componentsSeparatedByString: nsstrRowParse]];
Changing NSString to NSMutableString won't give you an NSMutableArray object. You will get an NSArray object just as in case of NSString. You will have to use that to get an NSMutableArray using the above method.
The componentsSeparatedByString method returns an NSArray. Try the following:
[NSMutableArray arrayWithArray:[nsstrFileContents componentsSeparatedByString: nsstrRowParse]];
I have a simple method to read a string and parse it to an array,
-(NSArray *) readFileToArray: (NSString *)file{
NSString *values = [NSString stringWithContentsOfFile: file];
NSArray *tokens = [values componentsSeparatedByString:#":"];
return tokens;
}
however instruments did report me I got a leak on NSString at line
NSArray *tokens = [values componentsSeparatedByString:#":"];
I have no idea why this happens,
1). I think both values and tokens are autoreleased? Am I right?
2). I tried to release values and tokens(just a try), it crashes.
Thanks for your help in advance.
Michael
The code you've posted is using correct memory management (the return value is autoreleased). Look at the code that is calling readFileToArray: to see how it's handling the returned array.
The line that is leaked is NSString *values = [NSString stringWithContentsOfFile: file];
You need to add autorelease in this line to fix the leak.