I have in the .h file :
NSString *dataHML;
NSString *dataHML2;
NSString *dataHML3;
NSString *dataHML4;
NSString *dataHML5;
NSString *dataHML6;
NSString *dataHMLtotal;
in the .m file I merge them with :
NSString *dataHtmlTotal = [NSString stringWithFormat:#"%#%#%#%#%#%#", dataHtml, dataHtml2, dataHtml3, dataHtml4,dataHtml5,dataHtml6];
But unfortunately it crashes at some point because of this.
Could anyone give me a other solution and post it please, because I already tried nsuserdefault or a nsarray, but without I couldn't get it working.
If you really do have 6 variables numerically named like that, you could be better off with an array.
NSMutableArray *dataHMLStrings = [NSMutableArray array];
[dataHMLStrings addObject:#"String1"];
[dataHMLStrings addObject:#"String2"];
.
.
.
[dataHMLStrings addObject:#"String100"]; // or however many you have.
NSString *dataHMLTotal = [dataHMLStrings componentsJoinedByString:#""];
You can give the componentsJoinedByString: method a different string (I passed an empty string here because you didn't want anything to appear between each dataHML string).
Please make sure your strings are all allocated and initialized (neither points of which you mention in your question.) If you do not do so then you run the risk of manipulating data at the location of the garbage pointers, and your application will very likely crash.
Related
Hi I've been looking all over and can't seem to find an answer to my issue. I have very little experience with programming, especially Objective-C.
In my app there is a plist with a certain number of strings. When the user taps a button, the program counts the number of strings in the plist, creates a random number within that range, and is supposed to read the line of the plist with that specific number.
Say if there were 20 strings in the plist, and the app generated the number 5, then the app is supposed to read the 5th string in the plist.
Is there a way to do such a thing or is there another, more efficient way to accomplish this? ANY help is GREATLY appreciated. Thanks.
Edit:
In my .h file I have
NSMutableArray* myArray;
int plistCount;
int randomNumber;
}
#property (nonatomic, retain) NSMutableArray* myArray;
And in my .m file I have
NSString *path = [[NSBundle mainBundle] pathForResource:#"myplist" ofType:#"plist"];
NSMutableArray* tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.myArray = tmpArray;
plistCount=[self.resorts count];
randomNumber=arc4random() % plistCount;
I dont know where to go from here.
NSString * randomString = self.myArray[arc4random_uniform(self.myArray.count)];
The above code is selecting a random element from self.myArray.
arc4random_uniform generates a number between 0 and the argument (excluded) so using self.myArray.count as a bound you'll get a valid random index for the array.
NOTE
You have to explicitly check that the array is never empty in order for the above code to work, since
arc4random_uniform(0) returns 0, therefore in case of an empty array it will generate a NSRangeException.
I am trying to store user input text (in this case a book title) into an array so that I can output it in a table view in another xib.
I'm getting stuck trying to store the "bookTitle.text" info into my "userinfoArray". I know it probably has a simple solution and I know how to do it in C++ but not in Objective-C. Any tips, links etc. would be great.
NSMutableArray *userinfoArray = [[NSMutableArray alloc]init];
NSString *tempString = [[NSString alloc]initWithString:[bookTitle text]];
[userinfoArray addObject:tempString];
you can then access it later with:
[userinfoArray objectAtIndex:0];
NSMutableArray is very flexible. with addObject:object you can add as many things as you want, remove them with removeObjectAtIndex:index.
more here: NSMutableArray Class Reference
alternatively if you know what size your array will have you can use a normal NSArray: NSArray Class Reference which will work similar
sebastian
Try
userinfoArray = [NSArray arrayWithObject:[bookTitle text]];
Or if you want to create a longer array with more objetcs then
userinfoArray = [NSArray arrayWithObjects:[bookTitle text], secondObject, thirdObject, nil];
If you want to add or remove objects later then you may want to use NSMutableArray instead.
If this does not answer your question, then please try to be a bit more specific about your problem.
I am new to Objective-C however I am slowly making progress. All I would like to do is append a new string to a current string. I need to do this for my NSURLConnection so that I can put all the data into one string. I currently have something like this, however it does work and I don't fully understand why and what I need to do to fix it.
NSString *temp = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
html = [htmlTemp stringByAppendingString:temp];
htmlTemp = html;
Thanks For any help I am sure its something to do with the memory however I don't fully understand that either.
StringByAppendingString is a convenience method that does not start with "new" or contains "alloc" or "copy", the returned string will be autoreleased most likely in the next run cycle, which is why when you try to read it again from html, it has already been released. To fix this you can send a copy message like this:
htmlTemp = [html copy];
I would recommend making htmlTemp a property like this:
#property (nonatomic, copy) NSString *htmlTemp;
This way when you assign a new value to it, using the setter, the old value will get released, before pointing to the new value that way you can do:
self.htmlTemp = [htmlTemp stringByAppendingString:temp];
In my Cocoa application, in the header file, I declare a NSString ivar:
NSString *gSdkPath;
Then, in awakeFromNib, I assign it to a value:
gSdkPath = #"hello";
Later, it's value is changed in the code:
gSdkPath = [NSString stringWithString:[folderNames objectAtIndex:0]];
(the object returned from objectAtIndex is an NSString)
However, after this point, in another method when I try to NSLog() (or do anything with) the gSdkPath variable, the app crashes. I'm sure this has something to do with memory management, but I'm beginning with Cocoa and not sure exactly how this all works.
Thanks for any help in advance.
EDIT: This was solved by retaining the string [gSdkPath retain].
(the object returned from
objectAtIndex is an NSString)
Are you sure? I suggest putting this in it's own temporary variable and double checking that it's not nil or invalid in some way.
Edit: If that is OK so far, do note that stringWithString returns an autoreleased object. You need to retain it if you want to use it "later".
gSdkPath = [NSString stringWithString:[folderNames objectAtIndex:0]];
[gSdkPath retain];
URL Download
http://code.google.com/p/mwiphonesdk/source/browse/#svn/trunk/iMADE/PrepTasks/08
I have code at the location at the link above and I am using NSMutableString to append strings as data is downloaded. But I am finding that using appendString does not increase the length of the mutable string. I must be doing something wrong.
And when I am done I need to convert NSMutableString to NSString to return it. I have looked for examples to do this but I do not see any yet.
I am most familiar with Java and C# where there is a StringBuffer/StringBuilder which allows you to append pieces and them simply call toString at the end to get a String. It does not appear to be this easy in Objective-C.
NSString* str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
#pragma mark TODO Confirm this is appending a value to the mutable string
[self.mutableString appendString:str];
NSLog(#"str length: %d, %d", [str length], [self.mutableString length]);
Above is the section of code that calls appendString.
I see that str has a length > 0 but calling appendString does not increase the length of self.mutableString.
What am I doing wrong here?
As for having an NSMutableString* and needing to return an NSString*, the former is a subclass of the latter so anywhere you see an NSString* an NSMutableString* will suffice as-is.
Your code looks OK from what you've posted. The only thing I can think of is that perhaps there isn't any data to speak of when initializing the str variable. In such a case appending an empty string will do nothing to mutableString.
You'll also want to make sure self.mutableString has been properly allocated and initialized. You can send messages to NSObject*s that are nil which may be misleading when [self.mutableString length] returns 0.
I have fixed the problem. I simply was not initializing the NSMutableString value and it was transparently not doing anything.
Before appending the string I put the following code.
if (_mutableString == nil){
_mutableString = [[NSMutableString alloc] init];
}
Thanks everyone for answering. And it is good to know that I can use NSMutableString in place of NSString. (that is too easy) :)