nsstring interview questions
Top nsstring frequently asked interview questions
Does anyone know how to use line breaks in NSString? I need to do something like this -
[NSString stringWithFormat:@"%@,\n%@", mystring1,mystring2];
Source: (StackOverflow)
I've been trying to get rid of the white spaces in an NSString
, but none of the methods I've tried worked.
I have "this is a test"
and I want to get "thisisatest"
.
I've used whitespaceCharacterSet
, which is supposed to eliminate the white spaces.
NSString *search = [searchbar.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
but I kept getting the same string with spaces. Any ideas?
Source: (StackOverflow)
NSString *myString = @"A B C D E F G";
I want to remove the spaces, so the new string would be "ABCDEFG".
Source: (StackOverflow)
What is the appropriate way of dealing with large text files in Objective-C? Let's say I need to read each line separately and want to treat each line as an NSString. What is the most efficient way of doing this?
One solution is using the NSString method:
+ (id)stringWithContentsOfFile:(NSString *)path
encoding:(NSStringEncoding)enc
error:(NSError **)error
and then split the lines with a newline separator, and then iterate over the elements in the array. However, this seems fairly inefficient. Is there no easy way to treat the file as a stream, enumerating over each line, instead of just reading it all in at once? Kinda like Java's java.io.BufferedReader.
Source: (StackOverflow)
Searched for answers, but the ones i found didn't seem to be iPhone specific.
I basically need to get current date and time separately, formatted as:
2009-04-26
11:06:54
Edit:
The code below,from another question on the same topic, generates
now: |2009-06-01 23:18:23 +0100|
dateString: |Jun 01, 2009 23:18|
parsed: |2009-06-01 23:18:00 +0100|
This is almost what i'm looking for, but i'd like to get just the date on one variable and just the time in another.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MMM dd, yyyy HH:mm"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:@"MMM dd, yyyy"];
NSDate *parsed = [inFormat dateFromString:dateString];
NSLog(@"\n"
"now: |%@| \n"
"dateString: |%@| \n"
"parsed: |%@|", now, dateString, parsed);
Source: (StackOverflow)
I have a URL string (NSString
) with spaces and &
characters. How do I url encode the entire string (including the &
ampersand character and spaces)?
Source: (StackOverflow)
How can I detect if a string contains a certain word? For example, I have a string below which reads:
@"Here is my string."
I'd like to know if I can detect a word in the string, such as "is" for example.
Source: (StackOverflow)
Can anybody point me in the right direction to be able to encrypt a string, returning another string with the encrypted data? (I've been trying with AES256 encryption.) I want to write a method which takes two NSString instances, one being the message to encrypt and the other being a 'passcode' to encrypt it with - I suspect I'd have to generate the encryption key with the passcode, in a way that can be reversed if the passcode is supplied with the encrypted data. The method should then return an NSString created from the encrypted data.
I've tried the technique detailed in the first comment on this post, but I've had no luck so far. Apple's CryptoExercise certainly has something, but I can't make sense of it... I've seen lots of references to CCCrypt, but it's failed in every case I've used it.
I would also have to be able to decrypt an encrypted string, but I hope that's as simple as kCCEncrypt/kCCDecrypt.
Source: (StackOverflow)
In my iOS 5 app, I have an NSString
that contains a JSON string. I would like to deserialize that JSON string representation into a native NSDictionary
object.
"{\"password\" : \"1234\", \"user\" : \"andreas\"}"
I tried the following approach:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:@"{\"2\":\"3\"}"
options:NSJSONReadingMutableContainers
error:&e];
But it throws the a runtime error. What am I doing wrong?
-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFConstantString bytes]: unrecognized selector sent to instance 0x1372c'
Source: (StackOverflow)
So I have an NSArray
"myArray" with NSNumber
s and NSString
s. I need them in another UIView
so i go like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *details = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:nil];
details.subjectText = [[myArray objectAtIndex:indexPath.row] objectForKey:@"subject"];
The subjectText works.
But how can I get the NSNumber
s out of it? (I actually need them as strings...)
I would convert a NSString
out of a NSNumber
like this:
NSString *blah = [NSNumber intValue]
. But I don't know how to set it up in the code above...
Source: (StackOverflow)
I have UTF-8 encoded NSData
from windows server and I want to convert it to NSString
for iPhone. Since data contains characters (like a degree symbol) which have different values on both platforms, how do I convert data to string?
Source: (StackOverflow)
I am wondering how to convert an NSArray [1,@"Hello",3,4,5]
to an string in Objective-C. An Applescript example is:
set the_array to {1,"Two", 3,4,5,6}
set the_array to the_array as string
Source: (StackOverflow)
I have a NSString like this:
http://www.
but I want to transform it to:
http%3A%2F%2Fwww.
How can I do this?
Source: (StackOverflow)