Questions tagged [nsstring]

NSString is the plain-text character-string class in Cocoa and Cocoa Touch. See also NSMutableString, NSData and NSMutableData (for objects that contain bytes rather than human-language characters), and NSAttributedString and NSMutableAttributedString (for rich-text strings).

NSString is the plain-text character-string class in Cocoa () and Cocoa Touch ().

An NSString is immutable, unless it's an NSMutableString (). NSMutableString is a subclass of NSString.

You will never work with a direct instance of NSString or NSMutableString. Both classes, like most of the other property-list classes, are members of a class cluster.

NSStrings contain human-language characters; you should not attempt to use an NSString to store bytes, including pixel data and XML or HTML markup. (“String” in Cocoa strictly refers to human text; it does not call a byte array a “string” like Python 2 and some other environments do.) To make or receive an object bytes, you should use NSData or NSMutableData (note that, for example, this is what NSXMLParser, WebFrame, and UIWebView take for the XML/HTML source of the document, and what NSURLConnection gives you as you download a resource).

An NSString is always plain text. For rich text, use NSAttributedString or its mutable subclass. Both have always been available on Mac OS X, and have been available on iOS since 3.2.

NSString is toll-free bridged with CFString, which means you can pass an NSString to CF functions expecting a CFString, and receive a CFString and treat it as an NSString. The same goes for the mutable subclass.

Docs:

7124 questions
61
votes
4 answers

convert NSDictionary to NSString

I am trying to put the content of an NSDictionary into an NSString for testing, But have no idea how to achieve this. Is it possible? if so how would one do such a thing? The reason I am doing this, Is I need to check the content of a NSDicitonary…
C.Johns
  • 10,185
  • 20
  • 102
  • 156
60
votes
8 answers

BOOL to NSString

If I have a method that returns a BOOL, how do I cast that to an NSString so I can print it out in console? For example, I tried doing this, which isn't working: NSLog(@"Is Kind of NSString:", ([thing isKindOfClass:[NSString class]]) ? @"YES" :…
Craig
  • 16,291
  • 12
  • 43
  • 39
60
votes
3 answers

How can I check that the NSString ends with the certain character(.jpg)?

I have a NSString object which is assigned this ("http://vspimages.vsp.virginia.gov/images/024937-02.jpg"). Can anybody tell me how to check whether the string ends with ".jpg"?
RAMAN RANA
  • 1,785
  • 4
  • 21
  • 40
60
votes
12 answers

Replace occurrences of space in URL

I have a URL in an iPhone application to work with. But the problem is that it has some spaces in the URL. I want to replace the spaces with '%20'. I know that there are the stringByReplacingOccurencesOfString and…
Joy
  • 1,609
  • 3
  • 16
  • 28
59
votes
8 answers

Calculating the number of days between two dates in Objective-C

Possible Duplicate: How can I compare two dates, return a number of days I have two dates (as NSString in the form "yyyy-mm-dd"), for example: NSString *start = "2010-11-01"; NSString *end = "2010-12-01"; I'd like to implement: -…
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
59
votes
13 answers

Number of occurrences of a substring in an NSString?

How can I get the number of times an NSString (for example, @"cake") appears in a larger NSString (for example, @"Cheesecake, apple cake, and cherry pie")? I need to do this on a lot of strings, so whatever method I use would need to be relatively…
igul222
  • 8,557
  • 14
  • 52
  • 60
55
votes
5 answers

How to parse NSString into BOOL in Objective-C?

I am programming in Objective-C for iOS. I would like to parse an object of type NSString into a scalar of type BOOL. I have a value, and I know that it will either be @"YES" or @"NO", but that YES (or) NO value is NSString and I just want to change…
Fire Fist
  • 7,032
  • 12
  • 63
  • 109
55
votes
9 answers

Using an NSString in a switch statement

Is it possible to use an NSString in a switch statement? Or is it better to just use if / else if?
Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224
54
votes
6 answers

Get the extension of a file contained in an NSString

I have an NSMutable dictionary that contains file IDs and their filename+extension in the simple form of fileone.doc or filetwo.pdf. I need to determine what type of file it is to correctly display a related icon in my UITableView. Here is what I…
jer-k
  • 1,413
  • 2
  • 12
  • 23
54
votes
5 answers

NSString: newline escape in plist

I'm writing a property list to be in the resources bundle of my application. An NSString object in the plist needs to have line-breaks in it. I tried \n, but that doesn't work. What do I do to have newlines in my string in the plist? Thanks.
Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79
54
votes
2 answers

How to insert new line \n from UILabel text which typed in IB?

I have id textInput and I insert new line(\n) OK with: [textInput insertText:@"\n"]; But when input Text from label.text (Input in Interface Builder) ,it NOT OK. Just input \n text. NSLog(@"%@",label.text); [textInput insertText:label.text]; How…
Alex
  • 1,161
  • 1
  • 8
  • 15
54
votes
8 answers

Objective-C 101 (retain vs assign) NSString

A 101 question Let's say i'm making database of cars and each car object is defined as: #import @interface Car:NSObject{ NSString *name; } @property(nonatomic, retain) NSString *name; Why is it @property(nonatomic, retain)…
qstar
  • 711
  • 1
  • 8
  • 11
53
votes
2 answers

Objective C - How to concatenate an entire array of strings?

I am an Objective C newbie. I want to write a method that takes in an array of strings and returns a concatenated string, with a comma (,) in between each string. So if an array is {a b c d}, I want to return a,b,c,d. What is the easiest way to do…
Suchi
  • 9,989
  • 23
  • 68
  • 112
53
votes
7 answers

Concatenate number with string in Swift

I need to concatenate a String and Int as below: let myVariable: Int = 8 return "first " + myVariable But it does not compile, with the error: Binary operator '+' cannot be applied to operands of type 'String' and 'Int' What is the proper way to…
Begum
  • 645
  • 1
  • 6
  • 11
52
votes
19 answers

Reverse NSString text

I have been googling so much on how to do this, but how would I reverse a NSString? Ex:hi would become: ih I am looking for the easiest way to do this. Thanks! @Vince I made this method: - (IBAction)doneKeyboard { // first retrieve the text of…
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191