EzDevInfo.com

nsstring interview questions

Top nsstring frequently asked interview questions

NSString with \n or line break

Does anyone know how to use line breaks in NSString? I need to do something like this -

[NSString stringWithFormat:@"%@,\n%@", mystring1,mystring2];

Source: (StackOverflow)

Remove all whitespaces from NSString

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)

Advertisements

Remove characters from NSString?

NSString *myString = @"A B C D E F G";

I want to remove the spaces, so the new string would be "ABCDEFG".


Source: (StackOverflow)

Objective-C: Reading a file line by line

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)

NSDate and NSDateFormatter - short format date and time in iphone sdk

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)

How do I URL encode a string

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)

Detecting if an NSString contains...?

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)

AES Encryption for an NSString on the iPhone

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)

How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)

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)

How to convert NSNumber to NSString

So I have an NSArray "myArray" with NSNumbers and NSStrings. 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 NSNumbers 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)

Convert UTF-8 encoded NSData to NSString

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)

Convert NSArray to NSString in Objective-C

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)

Objective-C url encoding

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)

How do I convert an NSString value to NSData?

How do I convert an NSString value to NSData?


Source: (StackOverflow)

String replacement in Objective-C

What is the best way to replace a character is a string in Objective-C for iPhone SDK?


Source: (StackOverflow)