NSString 클래스
Foundation Framework 의 NSString 클래스에 대해 알아보자.
Objective-C 에서 문자열을 다루는 표준클래스이다.
사용 형태
NSString *이름 = @"Ohl WOrld!";
특징
1) 내부적으로 문자열을 Unicode화 해서 사용하기 때문에 문자열 앞에 @를 표기.
2) NSString은 내용을 바꿀 수 없음.
3) 내용을 바꾸려면 NSString 클래스를 상속받은 가변형 NSString인 NSMutableString 클래스를 사용.
initWithBytes
stringWithFormat
initWithData:encoding:
rangeOfString
componentsSeparatedByString
등등 자주 쓰이는 메소드들이 있지만 따로 정리하진 않고, 관련 메소드들은 애플 도큐먼트를 참조하자.
Creating and Initializing Strings
+ string
Returns an empty string.
- init
Returns an initialized NSString
object that contains no characters.
- initWithBytes:length:encoding:
Returns an initialized NSString
object containing a given number of bytes from a given buffer of bytes interpreted in a given encoding.
- initWithBytesNoCopy:length:encoding:freeWhenDone:
Returns an initialized NSString
object that contains a given number of bytes from a given buffer of bytes interpreted in a given encoding, and optionally frees the buffer.
- initWithCharacters:length:
Returns an initialized NSString
object that contains a given number of characters from a given C array of UTF-16 code units.
- initWithCharactersNoCopy:length:freeWhenDone:
Returns an initialized NSString
object that contains a given number of characters from a given C array of UTF-16 code units.
- initWithString:
Returns an NSString
object initialized by copying the characters from another given string.
- initWithCString:encoding:
Returns an NSString
object initialized using the characters in a given C array, interpreted according to a given encoding.
- initWithUTF8String:
Returns an NSString
object initialized by copying the characters from a given C array of UTF8-encoded bytes.
- initWithFormat:
Returns an NSString
object initialized by using a given format string as a template into which the remaining argument values are substituted.
- initWithFormat:arguments:
Returns an NSString
object initialized by using a given format string as a template into which the remaining argument values are substituted without any localization. This method is meant to be called from within a variadic function, where the argument list will be available.
- initWithFormat:locale:
Returns an NSString
object initialized by using a given format string as a template into which the remaining argument values are substituted according to given locale.
- initWithFormat:locale:arguments:
Returns an NSString
object initialized by using a given format string as a template into which the remaining argument values are substituted according to given locale information. This method is meant to be called from within a variadic function, where the argument list will be available.
- initWithData:encoding:
Returns an NSString
object initialized by converting given data into UTF-16 code units using a given encoding.
+ stringWithFormat:
Returns a string created by using a given format string as a template into which the remaining argument values are substituted.
+ localizedStringWithFormat:
Returns a string created by using a given format string as a template into which the remaining argument values are substituted according to the current locale.
+ localizedUserNotificationStringForKey:arguments:
Returns a localized string intended for display in a notification alert.
+ stringWithCharacters:length:
Returns a string containing a given number of characters taken from a given C array of UTF-16 code units.
+ stringWithString:
Returns a string created by copying the characters from another given string.
+ stringWithCString:encoding:
Returns a string containing the bytes in a given C array, interpreted according to a given encoding.
Returns a string created by copying the data from a given C array of UTF8-encoded bytes.
Creating and Initializing a String from a File
+ stringWithContentsOfFile:encoding:error:
Returns a string created by reading data from the file at a given path interpreted using a given encoding.
- initWithContentsOfFile:encoding:error:
Returns an NSString
object initialized by reading data from the file at a given path using a given encoding.
+ stringWithContentsOfFile:usedEncoding:error:
Returns a string created by reading data from the file at a given path and returns by reference the encoding used to interpret the file.
Returns an NSString
object initialized by reading data from the file at a given path and returns by reference the encoding used to interpret the characters.
Creating and Initializing a String from an URL
+ stringWithContentsOfURL:encoding:error:
Returns a string created by reading data from a given URL interpreted using a given encoding.
- initWithContentsOfURL:encoding:error:
Returns an NSString
object initialized by reading data from a given URL interpreted using a given encoding.
+ stringWithContentsOfURL:usedEncoding:error:
Returns a string created by reading data from a given URL and returns by reference the encoding used to interpret the data.
Returns an NSString
object initialized by reading data from a given URL and returns by reference the encoding used to interpret the data.
Writing to a File or URL
- writeToFile:atomically:encoding:error:
Writes the contents of the receiver to a file at a given path using a given encoding.
- writeToURL:atomically:encoding:error:
Writes the contents of the receiver to the URL specified by url
using the specified encoding.
Getting a String's Length
The number of UTF-16 code units in the receiver.
- lengthOfBytesUsingEncoding:
Returns the number of bytes required to store the receiver in a given encoding.
- maximumLengthOfBytesUsingEncoding:
Returns the maximum number of bytes needed to store the receiver in a given encoding.
Getting Characters and Bytes
- characterAtIndex:
Returns the character at a given UTF-16 code unit index.
- getCharacters:range:
Copies characters from a given range in the receiver into a given buffer.
Gets a given range of characters as bytes in a specified encoding.
Getting C Strings
- cStringUsingEncoding:
Returns a representation of the receiver as a C string using a given encoding.
- getCString:maxLength:encoding:
Converts the receiver’s content to a given encoding and stores them in a buffer.
UTF8String
A null-terminated UTF8 representation of the string.
Identifying and Comparing Strings
Returns the result of invoking compare:
with NSCase
as the only option.
- localizedCaseInsensitiveCompare:
Compares the string with a given string using a case-insensitive, localized, comparison.
- compare:
Returns the result of invoking compare:
with no options and the receiver’s full extent as the range.
- localizedCompare:
Compares the string and a given string using a localized comparison.
- compare:options:
Compares the string with the specified string using the given options.
- compare:options:range:
Returns the result of invoking compare:
with a nil
locale.
- compare:options:range:locale:
Compares the string using the specified options and returns the lexical ordering for the range.
- localizedStandardCompare:
Compares strings as sorted by the Finder.
- hasPrefix:
Returns a Boolean value that indicates whether a given string matches the beginning characters of the receiver.
- hasSuffix:
Returns a Boolean value that indicates whether a given string matches the ending characters of the receiver.
- isEqualToString:
Returns a Boolean value that indicates whether a given string is equal to the receiver using a literal Unicode-based comparison.
hash
An unsigned integer that can be used as a hash table address.
Dividing Strings
- components
Returns an array containing substrings from the receiver that have been divided by a given separator.
- componentsSeparatedByCharactersInSet:
Returns an array containing substrings from the receiver that have been divided by characters in a given set.
- stringByTrimmingCharactersInSet:
Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
- substringFromIndex:
Returns a new string containing the characters of the receiver from the one at a given index to the end.
- substringWithRange:
Returns a string object containing the characters of the receiver that lie within a given range.
- substringToIndex:
Returns a new string containing the characters of the receiver up to, but not including, the one at a given index.
Finding Characters and Substrings
- containsString:
Returns whether the receiver contains a given string by performing a case-sensitive, locale-unaware search.
- localizedCaseInsensitiveContainsString:
Returns whether the receiver contains a given string by performing a case-insensitive, locale-aware search.
- localizedStandardContainsString:
Returns whether the receiver contains a given string by performing a case and diacritic insensitive, locale-aware search.
- rangeOfCharacterFromSet:
Finds and returns the range in the receiver of the first character from a given character set.
- rangeOfCharacterFromSet:options:
Finds and returns the range in the receiver of the first character, using given options, from a given character set.
- rangeOfCharacterFromSet:options:range:
Finds and returns the range in the receiver of the first character from a given character set found in a given range with given options.
- rangeOfString:
Finds and returns the range of the first occurrence of a given string within the receiver.
- rangeOfString:options:
Finds and returns the range of the first occurrence of a given string within the receiver, subject to given options.
- rangeOfString:options:range:
Finds and returns the range of the first occurrence of a given string, within the given range of the receiver, subject to given options.
- rangeOfString:options:range:locale:
Finds and returns the range of the first occurrence of a given string within a given range of the receiver, subject to given options, using the specified locale, if any.
- localizedStandardRangeOfString:
Finds and returns the range of the first occurrence of a given string within the receiver by performing a case and diacritic insensitive, locale-aware search.
- enumerateLinesUsingBlock:
Enumerates all the lines in a string.
- enumerateSubstringsInRange:options:usingBlock:
Enumerates the substrings of the specified type in the specified range of the string.
Replacing Substirngs
- stringByReplacingOccurrencesOfString:withString:
Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
- stringByReplacingOccurrencesOfString:withString:options:range:
Returns a new string in which all occurrences of a target string in a specified range of the receiver are replaced by another given string.
- stringByReplacingCharactersInRange:withString:
Returns a new string in which the characters in a specified range of the receiver are replaced by a given string.
Determining Line and Paragraph Ranges
- getLineStart:end:contentsEnd:forRange:
Returns by reference the beginning of the first line and the end of the last line touched by the given range.
- lineRangeForRange:
Returns the range of characters representing the line or lines containing a given range.
- getParagraphStart:end:contentsEnd:forRange:
Returns by reference the beginning of the first paragraph and the end of the last paragraph touched by the given range.
- paragraphRangeForRange:
Returns the range of characters representing the paragraph or paragraphs containing a given range.
Determinig Composed Character Sequences
- rangeOfComposedCharacterSequenceAtIndex:
Returns the range in the receiver of the composed character sequence located at a given index.
Returns the range in the string of the composed character sequences for a given range.
Converting String Contents Into a Property List
Parses the receiver as a text representation of a property list, returning an NSString
, NSData
, NSArray
, or NSDictionary
object, according to the topmost element.
- propertyListFromStringsFileFormat
Returns a dictionary object initialized with the keys and values found in the receiver.
Drawing Strings
- drawAtPoint:withAttributes:
Draws the receiver with the font and other display characteristics of the given attributes, at the specified point in the current graphics context.
- drawInRect:withAttributes:
Draws the attributed string inside the specified bounding rectangle.
- drawWithRect:options:attributes:context:
Draws the attributed string in the specified bounding rectangle using the provided options.
- boundingRectWithSize:options:attributes:context:
Calculates and returns the bounding rect for the receiver drawn using the given options and display characteristics, within the specified rectangle in the current graphics context.
Returns the bounding box size the receiver occupies when drawn with the given attributes.
Folding Strings
- stringByFoldingWithOptions:locale:
Returns a string with the given character folding options applied.
Getting a Shared Prefix
- commonPrefixWithString:options:
Returns a string containing characters the receiver and a given string have in common, starting from the beginning of each up to the first characters that aren’t equivalent.
Changing Case
A lowercase representation of the string.
localizedLowercaseString
Returns a version of the string with all letters converted to lowercase, taking into account the current locale.
- lowercaseStringWithLocale:
Returns a version of the string with all letters converted to lowercase, taking into account the specified locale.
uppercaseString
An uppercase representation of the string.
localizedUppercaseString
Returns a version of the string with all letters converted to uppercase, taking into account the current locale.
- uppercaseStringWithLocale:
Returns a version of the string with all letters converted to uppercase, taking into account the specified locale.
capitalizedString
A capitalized representation of the string.
localizedCapitalizedString
Returns a capitalized representation of the receiver using the current locale.
- capitalizedStringWithLocale:
Returns a capitalized representation of the receiver using the specified locale.
Getting Strings with Mapping
decomposed
A string made by normalizing the string’s contents using the Unicode Normalization Form D.
decomposedStringWithCompatibilityMapping
A string made by normalizing the receiver’s contents using the Unicode Normalization Form KD.
precomposedStringWithCanonicalMapping
A string made by normalizing the string’s contents using the Unicode Normalization Form C.
precomposedStringWithCompatibilityMapping
A string made by normalizing the receiver’s contents using the Unicode Normalization Form KC.
Getting Numeric Values
doubleValue
The floating-point value of the string as a double
.
floatValue
The floating-point value of the string as a float
.
intValue
The integer value of the string.
integerValue
The NSInteger
value of the string.
longLongValue
The long long
value of the string.
The Boolean value of the string.
Working with Encodings
Returns a zero-terminated list of the encodings string objects support in the application’s environment.
defaultCStringEncoding
Returns the C-string encoding assumed for any method accepting a C string as an argument.
+ stringEncodingForData:encodingOptions:convertedString:usedLossyConversion:
Returns the string encoding for the given data as detected by attempting to create a string according to the specified encoding options.
+ localizedNameOfStringEncoding:
Returns a human-readable string giving the name of a given encoding.
- canBeConvertedToEncoding:
Returns a Boolean value that indicates whether the receiver can be converted to a given encoding without loss of information.
- dataUsingEncoding:
Returns an NSData
object containing a representation of the receiver encoded using a given encoding.
- dataUsingEncoding:allowLossyConversion:
Returns an NSData
object containing a representation of the receiver encoded using a given encoding.
description
This NSString
object.
fastestEncoding
The fastest encoding to which the receiver may be converted without loss of information.
smallestEncoding
The smallest encoding to which the receiver can be converted without loss of information.
Working with Paths
Returns a string built from the strings in a given array by concatenating them with a path separator between each pair.
pathComponents
The file-system path components of the receiver.
- completePathIntoString:caseSensitive:matchesIntoArray:filterTypes:
Interprets the receiver as a path in the file system and attempts to perform filename completion, returning a numeric value that indicates whether a match was possible, and by reference the longest path that matches the receiver.
fileSystemRepresentation
A file system-specific representation of the receiver.
- getFileSystemRepresentation:maxLength:
Interprets the receiver as a system-independent path and fills a buffer with a C-string in a format and encoding suitable for use with file-system calls.
absolutePath
A Boolean value that indicates whether the receiver represents an absolute path.
lastPathComponent
The last path component of the receiver.
pathExtension
The path extension, if any, of the string as interpreted as a path.
stringByAbbreviatingWithTildeInPath
A new string that replaces the current home directory portion of the current path with a tilde (~
) character.
- stringByAppendingPathComponent:
Returns a new string made by appending to the receiver a given string.
- stringByAppendingPathExtension:
Returns a new string made by appending to the receiver an extension separator followed by a given extension.
stringByDeletingLastPathComponent
A new string made by deleting the last path component from the receiver, along with any final path separator.
stringByDeletingPathExtension
A new string made by deleting the extension (if any, and only the last) from the receiver.
stringByExpandingTildeInPath
A new string made by expanding the initial component of the receiver to its full path value.
stringByResolvingSymlinksInPath
A new string made from the receiver by resolving all symbolic links and standardizing path.
stringByStandardizingPath
A new string made by removing extraneous path components from the receiver.
- stringsByAppendingPaths:
Returns an array of strings made by separately appending to the receiver each string in in a given array.
Working with URLs
- stringByAddingPercentEncodingWithAllowedCharacters:
Returns a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.
Returns a new string made from the receiver by replacing all percent encoded sequences with the matching UTF-8 characters.
Linguistic Tagging and Analysis
- enumerate
Performs linguistic analysis on the specified string by enumerating the specific range of the string, providing the Block with the located tags.
- linguisticTagsInRange:scheme:options:orthography:tokenRanges:
Returns an array of linguistic tags for the specified range and requested tags within the receiving string.
'Objective-C 기초' 카테고리의 다른 글
구조체 Struct (0) | 2019.02.18 |
---|---|
블록 (Block) (0) | 2019.02.18 |
NSData 다루기 (바이너리 데이터 저장/복원) (0) | 2019.02.18 |
아카이빙(Archiving) (0) | 2019.02.16 |
프로토콜 (Protocol) (0) | 2019.02.15 |