I have got a little problem.
I would like to save an array to a textfile with this code:
for(int aa = 0; aa <= [lines2 count]; aa++) {
content = [[NSString alloc] initWithFormat:#"%#;%#", content, [lines2 objectAtIndex:aa]];
}
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"cart" ofType:#"txt"]; //meine Zeile
[content writeToFile:filePath atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
But it crashes my app. Can anybody give me an easy example, how to convert an NSMutable Array from
One
Two
Three
to an NSString like
One;Two;Three
Hope somebody can help me... :(
You should be able to do the concatenation using somewhat simpler code as below:
NSMutableString* content = [NSMutableString string];
for (int aa=0; aa < [lines2 count]; aa++){
[content appendString:[NSString stringWithFormat:#"%# ",[lines2 objectAtIndex:aa]]];
}
or, You could use componentsJoinedByString.
Maybe less, instead of less_or_equal?
for(int aa = 0; aa < [lines2 count]; aa++)
Related
I am trying to make a shorthand for [NSString stringWithFormat]:
How can I turn this (working):
#define fstring(s, ...) [NSString stringWithFormat:(s), ##__VA_ARGS__]
into this (with a check for nil value)
#define fstring(s, ...) [NSString stringWithFormat:(s), ##__VA_ARGS__ (__VA_ARGS__ ?:#"")]
Something like:
NSString *fullName = [NSString stringWithFormat:#"%#", contact.firstName ?: #""]
I have tried different shorthand combinations without luck... anybody got a better idea?
EDIT - Solution found
#define fstring(s, ...) [NSString stringWithFormat:(s), ##__VA_ARGS__ ?:#""]
Solution is the following:
#define fstring(s, ...) [NSString stringWithFormat:(s), ##__VA_ARGS__ ?:#""]
and if you want with empty string if null:
#define fstringOrEmpty(...) __VA_ARGS__ ? : #""
I am new at this so be gentle.
I have this function:
- (void) Morepoint {
gscore ++;
scoreString = [NSString stringWithFormat:#"%i", gscore];
lblscore.text = scoreString;
}
Where gscore is a global. scoreString is a NSString and lblscore is a label.
Every time I insert the function in my gameloop, the program stops to run.
Can anyone figure that out?
If I call the function from outside my gameloop, everything works fine, why?
You own the object returned from initWithFormat which you are responsible for releasing, but you don't own the object returned from stringWithFormat which returns an autoreleased string and so do not need to release it (if you do want to have ownership of it, you must retain it).
So for resolving your issue try to assign your value like this,
- (void) Morepoint
{
gscore ++;
scoreString = [[NSString alloc] initWithFormat:#"%i",gscore];
lblscore.text = scoreString;
}
Hope this helps you. Just Give it a try :)
How can I count the number of different characters used in a NSString.
Basically, from the string
Condimentum Consectetur Cras
I would like to get 14 as an answer because:
Condimet scura
We can't assume the NSString's content will always be English; a behaviour similar to [NSString length] for such cases - counting Unicode characters - is, therefore, expected.
NSString *oldString=#"THIS IS OLD STRING.";
NSMutableArray *charArray=[[[NSMutableArray alloc]init ] autorelease];
for (int k1=0; k1<[oldString length]; k1++) {
NSString *ichar = [NSString stringWithFormat:#"%c", [oldString characterAtIndex:k1]];
if (![charArray containsObject:ichar]) {
[charArray addObject:ichar];
}
}
NSMutableString *newString=[[NSMutableString alloc]init];
for (int k2=0; k2<[charArray count]; k2++) {
NSString *ichar =[charArray objectAtIndex:k2];
[newString appendString:ichar];
}
NSLog(#"old string lenght is %d",[oldString length]);
NSLog(#"new string lenght is %d",[newString length]);
Did You try this. I hope it will helps You.
Hi I've a problem with NSString floatValue function, if user insert more than six decimal number floatValue function Round data.
NSString *String = #"9.999999";
NSLog(#"Var Float: %f",[String floatValue]);
Result 9.999999
NSString *String = #"9.9999999";
NSLog(#"Var Float: %f",[String floatValue]);
Result 10 ??????
Someone can help me to solve it?
Thanks in advance!
You can't use float to manage number precision
Use NSDecimalNumber
NSString *Stringa = #"9.9999999";
NSDecimalNumber *Decimal = [[NSDecimalNumber alloc] initWithString:Stringa];
NSLog(#"Var Float: %f",[Decimal floatValue]);
NSLog(#"Var Float: %#",[Decimal stringValue]);
I'm doing a very simple trimming from a string. Basically it goes like this:
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *s = [[NSString alloc] initWithString:#"Some other string"];
s = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"%#",s);
[pool drain];
return 0;
}
No matter how many times I check, I can't find a mistake on the code. But the compiler keeps returning:
Running…
2009-12-30 08:49:22.086 Untitled[34904:a0f] Some other string
It doesn't trim the string at all. Is there a mistake in my code, because I can't believe something this simple doesn't work. Thanks!
Edit:
I think I've figured my mistake. stringByTrimmingCharactersInSet only trims the string from the leftmost and rightmost ends of a string. Can anybody confirm this?
You are correct - with your code stingByTrimmingCharactersInSet will trim the left and right whitespace but leave internal whitespace alone.
Note that there's also a memory leak in your code sample:
NSString *s = [[NSString alloc] initWithString:#"Some other string"];
This will leak when you re-assign to s in the next line.