How to plus NSString?
Example:
NSString *a=#"hello";
NSString *b=#"world";
NSString *s=#"";
I want:
s = "helloworld";
Thank for helping.
Please use below code.
NSString *s= [NSString stringWithFormat:#"%#%#", a, b];
What you are talking about is concatenating. And you need to use:
NSString *a = #"This ";
NSString *b = #"is a string";
NSString *c= [NSString stringWithFormat:#"%#%#", a, b];
Check out: objective-c strings a guide for beginners for a full list.
You can also use stringByAppendingString: like so -
NSString *completeString = [a stringByAppendingString:b];
NSString *a=#"hello";
NSString *b=#"world";
NSString *s=[NSString stringWithFormat:#"%#%#", a, b];
NSString *completeString = [a stringByAppendingString:b];
This function is Good and Easy...
Related
I want to convert NSData into NSString, my code is below.
NSData *data1 = [object valueForKey:#"college_list"];
NSString *myString =[ NSString stringWithCString:[data1 bytes] encoding:NSUTF8StringEncoding];
What is wrong with this code?
If 'data1' do have some thing in it. Try this:
[[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding]
May be there is some this wrong with your 'object'. Check its data is valid or issue is in its data.
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__ ? : #""
So I'm trying to play a .wave file in iOS5, and I'm getting a warning, which is leading to SIGABRTs and generally not-working code.
NSURL *soundUrl = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/Resources/Sounds/01_main_menu_list.wav", [[NSBundle mainBundle] resourcePath]]];
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[_audioPlayer setDelegate:self];
[_audioPlayer play];
This is giving me an error "Sending "ViewController *const __strong" to parameter of incompatible type 'id '.
I have literally no idea why, I've followed a half-dozen examples and am coming up empty. I'd love a pointer in the right direction.
This should work:
NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"01_main_menu_list" ofType:#"wav"]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioPlayer.delegate = self;
[audioPlayer play];
If not, I think there's something wrong with the wav file. Maybe the encoding?
Turned out it was an issue with ARC releasing, I fixed it by adding a #property and #synthesize pair for the AVAudioPlayer in question, and declaring it as strong. It got rid of all of these errors, and played the file with no problems.
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 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++)