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
1171
votes
30 answers

Shortcuts in Objective-C to concatenate NSStrings

Are there any shortcuts to (stringByAppendingString:) string concatenation in Objective-C, or shortcuts for working with NSString in general? For example, I'd like to make: NSString *myString = @"This"; NSString *test = [myString…
typeoneerror
  • 55,990
  • 32
  • 132
  • 223
1046
votes
14 answers

Constants in Objective-C

I'm developing a Cocoa application, and I'm using constant NSStrings as ways to store key names for my preferences. I understand this is a good idea because it allows easy changing of keys if necessary. Plus, it's the whole 'separate your data from…
Allyn
  • 20,271
  • 16
  • 57
  • 68
950
votes
14 answers

How do I convert an NSString value to NSData?

How do I convert an NSString value to NSData?
anish
689
votes
31 answers

How do I test if a string is empty in Objective-C?

How do I test if an NSString is empty in Objective-C?
Jamey McElveen
  • 18,135
  • 25
  • 89
  • 129
644
votes
27 answers

How do I check if a string contains another string in Swift?

In Objective-C the code to check for a substring in an NSString is: NSString *string = @"hello Swift"; NSRange textRange =[string rangeOfString:@"Swift"]; if(textRange.location != NSNotFound) { NSLog(@"exists"); } But how do I do this in…
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
581
votes
18 answers

How to convert an NSString into an NSNumber

How can I convert a NSString containing a number of any primitive data type (e.g. int, float, char, unsigned int, etc.)? The problem is, I don't know which number type the string will contain at runtime. I have an idea how to do it, but I'm not sure…
Enyra
  • 17,542
  • 12
  • 35
  • 44
575
votes
7 answers

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?
Ashwini Shahapurkar
  • 6,586
  • 6
  • 26
  • 35
467
votes
7 answers

How to add percent sign to NSString

I want to have a percentage sign in my string after a digit. Something like this: 75%. How can I have this done? I tried: [NSString stringWithFormat:@"%d\%", someDigit]; But it didn't work for me.
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
372
votes
14 answers

Trim spaces from end of a NSString

I need to remove spaces from the end of a string. How can I do that? Example: if string is "Hello " it must become "Hello"
Andrea Mario Lufino
  • 7,921
  • 12
  • 47
  • 78
367
votes
3 answers

"sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers" warning

I have Constants NSString, that I want to call like: [newString isEqualToString:CONSTANT_STRING]; Any wrong code here? I got this warning: sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers How should these be…
user4951
  • 32,206
  • 53
  • 172
  • 282
345
votes
7 answers

Convert an NSURL to an NSString

I have an app where the user can choose an image either from the built-in app images or from the iphone photo library. I use an object Occasion that has an NSString property to save the imagePath. Now in the case of the built-in app images I do get…
Ali
  • 4,205
  • 4
  • 25
  • 40
291
votes
16 answers

NSRange to Range

How can I convert NSRange to Range in Swift? I want to use the following UITextFieldDelegate method: func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string:…
János
  • 32,867
  • 38
  • 193
  • 353
287
votes
6 answers

String replacement in Objective-C

How to replace a character is a string in Objective-C?
4thSpace
  • 43,672
  • 97
  • 296
  • 475
263
votes
19 answers

Convert NSDate to NSString

How do I convert, NSDate to NSString so that only the year in @"yyyy" format is output to the string?
4thSpace
  • 43,672
  • 97
  • 296
  • 475
250
votes
12 answers

Case insensitive comparison NSString

Can anyone point me to any resources about case insensitive comparison in Objective C? It doesn't seem to have an equivalent method to str1.equalsIgnoreCase(str2)
Tejaswi Yerukalapudi
  • 8,987
  • 12
  • 60
  • 101
1
2 3
99 100