首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

objective-c NSString 施用详细指南

2012-08-24 
objective-c NSString 使用详细指南The above code declares a constant string object containing the wo

objective-c NSString 使用详细指南

The above code declares a constant string object containing the word "Hello" and calls the length method of object. The result is assigned to an integer variable named len which in turn is displayed using NSLog. When compiled and executed, we get the following output:

Length of string = 5

Constant string objects are actually instantiated from the NSConstantString class which, much like the other classes we will look at in this chapter, is actually a subclass of the NSString class. In practice, given the way that constant strings are used, it is unlikely that you will need to specifically declare your string constants as being of type NSConstantString. It is more likely that you will declare the string as we have done in this section and let the compiler handle the rest.

stringWithString class method of the NSMutableString class is then called, passing though the immutable string1 as an argument. This method returns a new object containing the immutable string and assigns it to string2. We now have a mutable copy of the immutable string1 object.

The above code will display the following output, proving that both string1 and string2 point to the same object since only one reference was modified, yet both show the change:

2009-11-03 14:35:37.731 t[32239:10b] string1 = This is a string and it is mine!2009-11-03 14:35:37.732 t[32239:10b] string2 = This is a string and it is mine!

To actually copy one string object to another string object we must use stringWithString method the NSMutableString class:

 NSMutableString *string1;NSMutableString *string2;string1 = [NSMutableString stringWithString: @"This is a string"]; // Initialize string1string2 = [NSMutableString stringWithString: string1]; // Copy string1 object to string2[string2 appendString: @" and it is mine!"]; // Modify string2NSLog (@"string1 = %@", string1);NSLog (@"string2 = %@", string2);

When executed, the appended text appears only in the object referenced by string2 since string2 now references a different object to that referenced by string1:

2009-11-03 14:42:10.426 t[32263:10b] string1 = This is a string2009-11-03 14:42:10.427 t[32263:10b] string2 = This is a string and it is mine!

NSMutableString *string1 = [NSMutableString stringWithString: @"The quick brown fox jumped"];[string1 replaceCharactersInRange: NSMakeRange(16, 3) withString: @"squirrel"];NSLog (@"string1 = %@", string1);

As you may have noted from the above example, the replacement string does not have to be the same length as the range being replaced. The string object and replacement method will resize the string automatically.

if (string1 == string2) test is asking whether the pointers point to the same memory location. Since string1 and string2 point to entirely different objects the answer, obviously, is no.

We can now take this a step further and change the code so that both string1 and string2 point to the same string object:

NSString *string1 = @"My String";NSString *string2;string2 = string1;if (string1 == string2)        NSLog (@"Strings match");else        NSLog (@"Strings do not match");

Now when we run the code, we get a "Strings match" result because both variables are pointing to the same object in memory.

To truly compare the actual strings contained within two string objects we must use the isEqualToString method:

NSString *string1 = @"My String";NSString *string2 = @"My String 2";if ([string1 isEqualToString: string2])        NSLog (@"Strings match");else        NSLog (@"Strings do not match");

Another option is to use the compare method (to perform a case sensitive comparison) or the caseInsenstiveCompare NSString methods. These are more advanced comparison methods that can be useful when sorting strings into order.

boolean values based on whether a match is found or not.

NSString *string1 = @"The quick brown fox jumped";BOOL result;result = [string1 hasPrefix: @"The"];if (result)        NSLog (@"String begins with The");result = [string1 hasSuffix: @"dog"];if (result)        NSLog (@"String ends with dog");

methods returns a new string object reflecting the change, leaving the original string object unchanged.

capitalizedString

Returns a copy of the specified string with the first letter of each word capitalized and all other characters in lower case:

NSString *string1 = @"The quicK brOwn fox jumpeD";NSString *string2;string2 = [string1 capitalizedString];

The above code will return a string object containing the string "The Quick Brown Fox Jumped" and assign it to the string2 variable. The string object referenced by string1 remains unmodified.

lowercaseString

Returns a copy of the specified string with all characters in lower case:

NSString *string1 = @"The quicK brOwn fox jumpeD";NSString *string2;string2 = [string1 lowercaseString];

The above code will return a string object containing the string "the quick brown fox jumped" and assign it to the string2 variable. The string object referenced by string1 remains unmodified.

uppercaseString

Returns a copy of the specified string with all characters in upper case:

NSString *string1 = @"The quicK brOwn fox jumpeD";NSString *string2;string2 = [string1 uppercaseString];

The above code will return a string object containing the string "THE QUICK BROWN FOX JUMPED" and assign it to the string2 variable. The string object referenced by string1 remains unmodified.

ASCII C style character string using the UTF8String method. For example:

NSString *string1 = @"The quick browen fox";const char *utfString = [string1 UTF8String];printf ("Converted string = %s\n", utfString);
1 楼 chenniaoc 2010-09-02   http://www.techotopia.com/index.php/Working_with_String_Objects_in_Objective-C

热点排行