item change NSMutableArray - objective-c

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];

Related

NSString starting with # using componentsSeparatedByString does not work

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:#":"];

[__NSCFString count]: Unrecognized selector

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);

Why is NSString stringWithString returning pointer to copied string?

I'm trying to copy an NSString value out of an NSMutableArray into a new variable. NSString stringWithString is returning an NSString with the same memory address as the object in my array. Why?
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSMutableArray *arr = [NSMutableArray arrayWithObject:#"first"];
NSLog(#"string is '%#' %p", [arr objectAtIndex:0], [arr objectAtIndex:0]);
// copy the string
NSString *copy = [NSString stringWithString:[arr objectAtIndex:0]];
NSLog(#"string is '%#' %p", copy, copy);
}
return 0;
}
1) Whenever you're creating a string using the #"" syntax, the framework will automatically cache the string. NSString is a very special class, but the framework will take care of it. When you use #"Some String" in multiple places of your app, they will all point to the same address in memory. Only when you're using something like -initWithData:encoding, the string won't be cached.
2) The other answers suggested that you should use -copy instead, but -copy will only create a copy of the object if the object is mutable. (like NSMutableString)
When you're sending -copy to an immutable object (like NSString), it'll be the same as sending it -retain which returns the object itself.
NSString *originalString = #"Some String";
NSString *copy = [originalString copy];
NSString *mutableCopy1 = [originalString mutableCopy];
NSString *mutableCopy2 = [mutableCopy copy];
NSString *anotherString = [[NSString alloc] initWithString:originalString];
--> originalString, copy, mutableCopy2 and anotherString will all point to the same memory address, only mutableCopy1 points do a different region of memory.
Since NSString instances are not mutable, the +stringWithString: method is simply returning the input string with an incremented reference count.
If you really want to force the creating of a new, identical string, try:
NSString * copy = [NSString stringWithFormat:#"%#", [arr objectAtIndex:0]];
There is little point in doing so, though, unless you need the pointer to be unique for some other reason...

Editing a plist in NSDictionary

I'm importing a plist into an NSDictionary object and want to change the contents of the plist once in the dictionary to lowercase. I have tried several different ways with no luck. Maybe I am supposed to edit the strings that are inside the NSDictionary, but then I don't know how to do that.
What I have currently imports the plist correctly, but the lowercaseString line effectively does nothing.
NSString *contents = [[NSBundle mainBundle] pathForResource:#"Assets/test" ofType:#"plist"];
NSString *newContents = [contents lowercaseString];
NSDictionary *someDic = [NSDictionary dictionaryWithContentsOfFile:newContents];
for (NSString *someData in someDic) {
NSLog(#"%# relates to %#", someData, [someDic objectForKey:someData]);
}
I NSLog'd the "content" string and it gave me a path value, so obviously changing that to a lowercaseString wouldn't do anything. I'm feeling like I have to access the strings within the dictionary.
This line
NSString *newContents = [contents lowercaseString];
is change the path string returned from
NSString *contents = [[NSBundle mainBundle] pathForResource:#"Assets/test" ofType:#"plist"];
so you will end up with something like ../assets/test.plist
you will need to walk through the contents of someDic an create a new dictionary based on the old turning strings into lowercase, if you are only concerned about values directly in someDic you can do something like
for( NSString * theKey in someDic )
{
id theObj = [someDic objectForKey:theKey];
if( [theObj isKindOfClass:[NSString class]] )
theObj = [theObj lowercaseString];
[theNewDict setObject:theObj forKey:theKey];
}

I am having trouble with compatibility between NSArray and NSMutableArray?

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]];