阅读背景:

菜鸟:IOS 学习:煎饼记(第十天)

来源:互联网 

一、NSString.h文件所在
    1、    IOS  

       

一、NSString.h文件所在
    1、    IOS  

       $1  /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h
       $2 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h
    2、 mac 
      /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSString.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSString.h

 

二 、NSString.h分析

 

/* NSString.h
Copyright (c) 1994-2012, Apple Inc. All rights reserved.
*/


typedef unsigned
short unichar; 短正值整型,不少于16位


#import <limits.h>
#import <Foundation/NSObject.h> 根类,提供基本运行能力的接口
#import <Foundation/NSRange.h> 截取字符串或者数组
#import <stdarg.h> C标准函数库的头文件,stdarg主要目的是为让函数能够接收不定参数


@class NSData, NSArray, NSDictionary, NSCharacterSet, NSURL, NSError, NSLocale; //@class 告诉编译器,其后面声明的名称是类的名称


FOUNDATION_EXPORT NSString
* const NSParseErrorException; // raised by -propertyList 异常处理处理串定义


#define NSMaximumStringLength (INT_MAX-1) //字符串最大值定义


/* These options apply to the various search/find and comparison methods (except where noted). * 下面部分主要用于搜索,查找,比较
*/
typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
NSCaseInsensitiveSearch
= 1, //不区分大小写比较
NSLiteralSearch = 2, /* Exact character-by-character equivalence */ //区分大小写比较
NSBackwardsSearch = 4, /* Search from end of source string */ //从字符串末尾开始搜索
NSAnchoredSearch = 8, /* Search is limited to start (or end, if NSBackwardsSearch) of source string */ 搜索限制范围的字符串
NSNumericSearch
= 64, /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find *//按照字符串里的数字为依据,算出顺序
NSDiacriticInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0)
= 128, /* If specified, ignores diacritics (o-umlaut == o) */ //忽略 "-" 符号的比较
NSWidthInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 256, /* If specified, ignores width differences ('a' == UFF41) *//忽略字符串的长度,比较出结果
NSForcedOrderingSearch NS_ENUM_AVAILABLE(10_5, 2_0)
= 512, /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */ 忽略不区分大小写比较的选项,并强制返回
NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7, 3_2)
= 1024 /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */
};


/* Note that in addition to the values explicitly listed below, NSStringEncoding supports encodings provided by CFString.
See CFStringEncodingExt.h for a list of these encodings.
See CFString.h for functions which convert between NSStringEncoding and CFStringEncoding.
*/
enum {
NSASCIIStringEncoding
= 1, /* 0..127 only */
NSNEXTSTEPStringEncoding
= 2,
NSJapaneseEUCStringEncoding
= 3,
NSUTF8StringEncoding
= 4,
NSISOLatin1StringEncoding
= 5,
NSSymbolStringEncoding
= 6,
NSNonLossyASCIIStringEncoding
= 7,
NSShiftJISStringEncoding
= 8, /* kCFStringEncodingDOSJapanese */
NSISOLatin2StringEncoding
= 9,
NSUnicodeStringEncoding
= 10,
NSWindowsCP1251StringEncoding
= 11, /* Cyrillic; same as AdobeStandardCyrillic */
NSWindowsCP1252StringEncoding
= 12, /* WinLatin1 */
NSWindowsCP1253StringEncoding
= 13, /* Greek */
NSWindowsCP1254StringEncoding
= 14, /* Turkish */
NSWindowsCP1250StringEncoding
= 15, /* WinLatin2 */
NSISO2022JPStringEncoding
= 21, /* ISO 2022 Japanese encoding for e-mail */
NSMacOSRomanStringEncoding
= 30,


NSUTF16StringEncoding
= NSUnicodeStringEncoding, /* An alias for NSUnicodeStringEncoding */


NSUTF16BigEndianStringEncoding
= 0x90000100, /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF16LittleEndianStringEncoding
= 0x94000100, /* NSUTF16StringEncoding encoding with explicit endianness specified */


NSUTF32StringEncoding
= 0x8c000100,
NSUTF32BigEndianStringEncoding
= 0x98000100, /* NSUTF32StringEncoding encoding with explicit endianness specified */
NSUTF32LittleEndianStringEncoding
= 0x9c000100 /* NSUTF32StringEncoding encoding with explicit endianness specified */
};
typedef NSUInteger NSStringEncoding;


typedef NS_OPTIONS(NSUInteger, NSStringEncodingConversionOptions) {
NSStringEncodingConversionAllowLossy
= 1,
NSStringEncodingConversionExternalRepresentation
= 2
};


FOUNDATION_EXPORT NSString
* const NSCharacterConversionException;




@interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> 协议


/* NSString primitive (funnel) methods. A minimal subclass of NSString just needs to implement these, although we also recommend getCharacters:range:. See below for the other methods.
*/
- (NSUInteger)length; 返回字符串的长度
- (unichar)characterAtIndex:(NSUInteger)index; 返回在字符串中的某个位置的字符


@end


@interface NSString (NSStringExtensionMethods) 类别


- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange; unichar是两字节长的char,代表unicode的一个字符,截取字符串指定段- (NSString *)substringFromIndex:(NSUInteger)from; 截取指定位置后面的字符串
- (NSString *)substringToIndex:(NSUInteger)to; 截取指定长度的字符串,从索引0开始
- (NSString *)substringWithRange:(NSRange)range; // Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters 截取字符串指定段,返回类型NSString


/* In the compare: methods, the range argument specifies the subrange, rather than the whole, of the receiver to use in the comparison. The range is not applied to the search string. For example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)] compares "A" to "ABC", not "A" to "A", and will return NSOrderedAscending.
*/
- (NSComparisonResult)compare:(NSString *)string; 比较字符串
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; 以某些条件比较字符串
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange; 以某些条件比较字符串至指定段
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; // locale arg used to be a dictionary pre-Leopard. We now accept NSLocale. Assumes the current locale if non-nil and non-NSLocale. nil continues to mean canonical compare, which doesn't depend on user's locale choice. 以某些限制+本地化(语言环境)比较条件比较字符串至指定段
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;不区别大小比较
- (NSComparisonResult)localizedCompare:(NSString *)string;本地化比较
- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string; 不区别大小本地化比较


/* localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.
*/
- (NSComparisonResult)localizedStandardCompare:(NSString *)string NS_AVAILABLE(10_6, 4_0);

- (BOOL)isEqualToString:(NSString *)aString; 测试两个字符串是否相等
- (BOOL)hasPrefix:(NSString *)aString; 测试字符串是否以 nsstring 开始
- (BOOL)hasSuffix:(NSString *)aString; 测试字符串是否以 nsstring 结尾


/* These methods return length==0 if the target string is not found. So, to check for containment: ([str rangeOfString:@"target"].length > 0). Note that the length of the range returned by these methods might be different than the length of the target string, due composed characters and such.
*/
typedef
struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
- (NSRange)rangeOfString:(NSString *)aString; 搜索字符串One是否存在于字符串Two
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask; 以某些限制条件搜索字符串One是否存在于字符串Two
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件搜索字符串One是否存在于字符串Two指定位置段
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);以某些限制条件+本地化限制搜索字符串One是否存在于字符串Two指定位置段


/* These return the range of the first character from the set in the string, not the range of a sequence of characters.
*/
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet; 指定字符集搜索
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask;以某些限制条件指定字符集搜索
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件指定字符集搜索指定位置段


- (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index; 以字符串的字符编码指定索引查找位置
- (NSRange)rangeOfComposedCharacterSequencesForRange:(NSRange)range NS_AVAILABLE(10_5, 2_0); 以字符串的字符编码指定区域段查找位置


- (NSString *)stringByAppendingString:(NSString *)aString; 将字符串One添加字符串Two后面
- (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 将多个字符串添加字符串Two后面


/* The following convenience methods all skip initial space characters (whitespaceSet) and ignore trailing characters. NSScanner can be used for more "exact" parsing of numbers.
*/
- (double)doubleValue; 返回转化的double类型
- (float)floatValue; 返回转化的float类型
- (int)intValue; 返回转化的int类型
- (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0); 返回转化的NSInteger(32位系统NSInteger是一个int,即32位,但当时64位系统时,NSInteger便是64位的)类型
- (long long)longLongValue NS_AVAILABLE(10_5, 2_0); 返回转化的 长int类型
- (BOOL)boolValue NS_AVAILABLE(10_5, 2_0); // Skips initial space characters (whitespaceSet), or optional -/+ sign followed by zeroes. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9. It ignores any trailing characters. 返回转化的 长BOOL类型


- (NSArray *)componentsSeparatedByString:(NSString *)separator; 字符串转化为数组
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0); 依据字符编码,分割字符串


- (NSString *)commonPrefixWithString:(NSString *)aString options:(NSStringCompareOptions)mask;


/* The following three case methods perform the canonical (non-localized) mappings. They are suitable for programming operations that require stable results not depending on the user's locale preference. For localized case mapping for strings presented to users, use their corresponding methods with locale argument below.
*/
- (NSString *)uppercaseString; 所有字符转化为大写
- (NSString *)lowercaseString; 所有字符转化为小写
- (NSString *)capitalizedString; 所有单词首字母转化大写


/* The following methods perform localized case mappings based on the locale specified. Passing nil indicates the canonical mapping. For the user preference locale setting, specify +[NSLocale currentLocale].
*/
- (NSString *)uppercaseStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0); 所有字符转化为大写(本地化)
- (NSString *)lowercaseStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0);所有字符转化为小写(本地化)
- (NSString *)capitalizedStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0); 所有单词首字母转化大写(本地化)


- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set; 字符串替换
- (NSString *)stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex;将字符串two指定索引段添加指定索引段字符串One

- (void)getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range; 指定段分行去字符串
- (NSRange)lineRangeForRange:(NSRange)range; 返回字符串指定段的位置和长度


- (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;指定段分段取字符串
- (NSRange)paragraphRangeForRange:(NSRange)range; 指定段分段的位置和长度

#if NS_BLOCKS_AVAILABLE
typedef NS_OPTIONS(NSUInteger, NSStringEnumerationOptions) {
// Pass in one of the "By" options:
NSStringEnumerationByLines = 0, // Equivalent to lineRangeForRange:
NSStringEnumerationByParagraphs = 1, // Equivalent to paragraphRangeForRange:
NSStringEnumerationByComposedCharacterSequences = 2, // Equivalent to rangeOfComposedCharacterSequencesForRange:
NSStringEnumerationByWords = 3,
NSStringEnumerationBySentences
= 4,
// ...and combine any of the desired additional options:
NSStringEnumerationReverse = 1UL << 8,
NSStringEnumerationSubstringNotRequired
= 1UL << 9,
NSStringEnumerationLocalized
= 1UL << 10 // User's default locale
};


/* In the enumerate methods, the blocks will be invoked inside an autorelease pool, so any values assigned inside the block should be retained.
*/
- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 检查是否在指定范围内是否有匹配的字符串
- (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 枚举字符串所有行
#endif


- (NSString *)description; 返回字符串


- (NSUInteger)hash; 返回字符串哈希地址


/*** Encoding methods ***/


- (NSStringEncoding)fastestEncoding; // Result in O(1) time; a rough estimate 字符串最快编码值
- (NSStringEncoding)smallestEncoding; // Result in O(n) time; the encoding in which the string is most compact 字符串最小编码值


- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy; // External representation 返回指定编码的NSData对象
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding; // External representation 返回指定编码的NSData对象


- (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding; 判断是否可以无损转化编码


/* Methods to convert NSString to a NULL-terminated cString using the specified encoding. Note, these are the "new" cString methods, and are not deprecated like the older cString methods which do not take encoding arguments.
*/
- (__strong const char *)cStringUsingEncoding:(NSStringEncoding)encoding NS_RETURNS_INNER_POINTER; // "Autoreleased"; NULL return if encoding conversion not possible; for performance reasons, lifetime of this should not be considered longer than the lifetime of the receiving string (if the receiver string is freed, this might go invalid then, before the end of the autorelease scope) char编码转换
- (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding; // NO return if conversion not possible due to encoding errors or too small of a buffer. The buffer should include room for maxBufferCount bytes; this number should accomodate the expected size of the return value plus the NULL termination character, which this method adds. (So note that the maxLength passed to this method is one more than the one you would have passed to the deprecated getCString:maxLength:.) c字符串编码转化是否成功


/* Use this to convert string section at a time into a fixed-size buffer, without any allocations. Does not NULL-terminate.
buffer is the buffer to write to; if NULL, this method can be used to computed size of needed buffer.
maxBufferCount is the length of the buffer in bytes. It's a good idea to make sure this is at least enough to hold one character's worth of conversion.
usedBufferCount is the length of the buffer used up by the current conversion. Can be NULL.
encoding is the encoding to convert to.
options specifies the options to apply.
range is the range to convert.
leftOver is the remaining range. Can be NULL.
YES return indicates some characters were converted. Conversion might usually stop when the buffer fills,
but it might also stop when the conversion isn't possible due to the chosen encoding.
*/
- (BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger *)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover; 指定缓存区转换


/* These return the maximum and exact number of bytes needed to store the receiver in the specified encoding in non-external representation. The first one is O(1), while the second one is O(n). These do not include space for a terminating null.
*/
- (NSUInteger)maximumLengthOfBytesUsingEncoding:(NSStringEncoding)enc; // Result in O(1) time; the estimate may be way over what's needed. Returns 0 on error (overflow) 字符串编码时需要用的字节长度
- (NSUInteger)lengthOfBytesUsingEncoding:(NSStringEncoding)enc; // Result in O(n) time; the result is exact. Returns 0 on error (cannot convert to specified encoding, or overflow)字符串编码时需要用到最大字节长度

FormC 指示使用完全规范化分解对 Unicode 字符串进行规范化,然后将序列替换为其原复合字符(如果可能)。FormD 指示使用完全规范化分解对 Unicode 字符串进行规范化。FormKC 指示使用完全兼容性分解对 Unicode 字符串进行规范化,然后将序列替换为其原复合字符(如果可能)。FormKD 指示使用完全兼容性分解对 Unicode 字符串进行规范化。
- (NSString *)decomposedStringWithCanonicalMapping; Unicode范式D标准化
- (NSString *)precomposedStringWithCanonicalMapping; Unicode范式C标准化
- (NSString *)decomposedStringWithCompatibilityMapping; Unicode范式KD标准化
- (NSString *)precomposedStringWithCompatibilityMapping; Unicode范式KC标准化


/* Returns a string with the character folding options applied. theOptions is a mask of compare flags with *InsensitiveSearch suffix.
*/
- (NSString *)stringByFoldingWithOptions:(NSStringCompareOptions)options locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0); 本地化字符串折叠


/* Replace all occurrences of the target string in the specified range with replacement. Specified compare options are used for matching target. If NSRegularExpressionSearch is specified, the replacement is treated as a template, as in the corresponding NSRegularExpression methods, and no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch.
*/
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange NS_AVAILABLE(10_5, 2_0); 字符串指定区域段替换


/* Replace all occurrences of the target string with replacement. Invokes the above method with 0 options and range of the whole string.
*/
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0); 字符串替换


/* Replace characters in range with the specified string, returning new string.
*/
- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0); 指定区域段字符串替换


- (__strong const char *)UTF8String NS_RETURNS_INNER_POINTER; // Convenience to return null-terminated UTF8 representation 转化为char


/* User-dependent encoding who value is derived from user's default language and potentially other factors. The use of this encoding might sometimes be needed when interpreting user documents with unknown encodings, in the absence of other hints. This encoding should be used rarely, if at all. Note that some potential values here might result in unexpected encoding conversions of even fairly straightforward NSString content --- for instance, punctuation characters with a bidirectional encoding.
*/
+ (NSStringEncoding)defaultCStringEncoding; // Should be rarely used 默认C字符串编码


+ (const NSStringEncoding *)availableStringEncodings; 当前编码值
+ (NSString *)localizedNameOfStringEncoding:(NSStringEncoding)encoding; 编码的名称


/*** Creation methods ***/


/* In general creation methods in NSString do not apply to subclassers, as subclassers are assumed to provide their own init methods which create the string in the way the subclass wishes. Designated initializers of NSString are thus init and initWithCoder:.
*/
- (id)init; 初始化NSSting 对象
- (id)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer; /* "NoCopy" is a hint */ 指定缓冲区,编码和字节长度初始化NSSting 对象
- (id)initWithCharacters:(const unichar *)characters length:(NSUInteger)length; 指定unichar字符,字节长度初始化NSSting 对象
- (id)initWithUTF8String:(const char *)nullTerminatedCString; char 转化为NSString对象
- (id)initWithString:(NSString *)aString; 指定字符串初始化NSSting 对象
- (id)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);格式化多个字符串初始化NSSting 对象
- (id)initWithFormat:(NSString *)format arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0); 格式化字符串初始化NSSting 对象
- (id)initWithFormat:(NSString *)format locale:(id)locale, ... NS_FORMAT_FUNCTION(1,3);本地化格式化多个字符串初始化NSSting 对象
- (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);本地化格式化字符串初始化NSSting 对象
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding; 指定编码转化NSData数据
- (id)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding; 指定编码和字节数初始化NSString对象
- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer;指定缓冲区,编码和字节数初始化NSString对象


+ (id)string; 初始化空string对象
+ (id)stringWithString:(NSString *)string;初始化string对象
+ (id)stringWithCharacters:(const unichar *)characters length:(NSUInteger)length; 返回指定长度unichar的C字符串
+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 转化C字符串为UTF8串
+ (id)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 格式化出初始化NSString对象,自动释放内存
+ (id)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 本地化格式化出初始化NSString对象,自动释放内存


- (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding; 指定编码初始化C字符串,需要手动释放内存
+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc; 指定编码初始化C字符串,自动释放内存


/* These use the specified encoding. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).
*/
- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取URL地址数据转换为字符串,需要手动释放内存(已知编码)
- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取FILE地址数据转换为字符串,需要手动释放内存(已知编码)
+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取URL地址数据转换为字符串,自动释放内存(已知编码)
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,自动释放内存(已知编码)


/* These try to determine the encoding, and return the encoding which was used. Note that these methods might get "smarter" in subsequent releases of the system, and use additional techniques for recognizing encodings. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).
*/
- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取URL地址数据转换为字符串,需要手动释放内存(未知编码)
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,需要手动释放内存(未知编码)
+ (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取URL地址数据转换为字符串,自动释放内存(未知编码)
+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,自动释放内存(未知编码)


/* Write to specified url or path using the specified encoding. The optional error return is to indicate file system or encoding errors.
*/
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码把数据写入文件
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码把数据写入文件


@end




@interface NSMutableString : NSString


/* NSMutableString primitive (funnel) method. See below for the other mutation methods.
*/
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString; 使用 aString 替换 range 指定的区域段


@end


@interface NSMutableString (NSMutableStringExtensionMethods)


- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc; 以索引loc 为起始位置插入 aString
- (void)deleteCharactersInRange:(NSRange)range; 删除指定区域段字符
- (void)appendString:(NSString *)aString; 追加字符串aString
- (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 追加多个字符串(格式化)
- (void)setString:(NSString *)aString; 设置新的 aString


/* In addition to these two, NSMutableString responds properly to all NSString creation methods.
*/
- (id)initWithCapacity:(NSUInteger)capacity; 初始化一个容量为capacity的字符串,需要手动释放内存
+ (id)stringWithCapacity:(NSUInteger)capacity;初始化一个容量为capacity的字符串,自动释放内存( 暂且分配指定容量,用于内存优化,可以大于设置的容量)


/* This method replaces all occurrences of the target string with the replacement string, in the specified range of the receiver string, and returns the number of replacements. NSBackwardsSearch means the search is done from the end of the range (the results could be different); NSAnchoredSearch means only anchored (but potentially multiple) instances will be replaced. NSLiteralSearch and NSCaseInsensitiveSearch also apply. NSNumericSearch is ignored. Use NSMakeRange(0, [receiver length]) to process whole string. If NSRegularExpressionSearch is specified, the replacement is treated as a template, as in the corresponding NSRegularExpression methods, and no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch.
*/
- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
指定限制条件(大小忽略等)指定区段中的replacement替换成target

@end






@interface NSString (NSExtendedStringPropertyListParsing)


/* These methods are no longer recommended since they do not work with property lists and strings files in binary plist format. Please use the APIs in NSPropertyList.h instead.
*/
- (id)propertyList;字符串转化为属性列表
- (NSDictionary *)propertyListFromStringsFileFormat;字符串转化为字典


@end






@interface NSString (NSStringDeprecated)


/* The following methods are deprecated and will be removed from this header file in the near future. These methods use [NSString defaultCStringEncoding] as the encoding to convert to, which means the results depend on the user's language and potentially other settings. This might be appropriate in some cases, but often these methods are misused, resulting in issues when running in languages other then English. UTF8String in general is a much better choice when converting arbitrary NSStrings into 8-bit representations. Additional potential replacement methods are being introduced in NSString as appropriate.
*/
- (const char *)cString NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (const char *)lossyCString NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (NSUInteger)cStringLength NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (void)getCString:(char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (void)getCString:(char *)bytes maxLength:(NSUInteger)maxLength NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (void)getCString:(char *)bytes maxLength:(NSUInteger)maxLength range:(NSRange)aRange remainingRange:(NSRangePointer)leftoverRange NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);


- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);


- (id)initWithContentsOfFile:(NSString *)path NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (id)initWithContentsOfURL:(NSURL *)url NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
+ (id)stringWithContentsOfFile:(NSString *)path NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
+ (id)stringWithContentsOfURL:(NSURL *)url NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);


- (id)initWithCStringNoCopy:(char *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (id)initWithCString:(const char *)bytes length:(NSUInteger)length NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (id)initWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
+ (id)stringWithCString:(const char *)bytes length:(NSUInteger)length NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
+ (id)stringWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);


/* This method is unsafe because it could potentially cause buffer overruns. You should use -getCharacters:range: instead.
*/
- (void)getCharacters:(unichar *)buffer;


@end


enum {
NSProprietaryStringEncoding
= 65536 /* Installation-specific encoding */
};






/* The rest of this file is bookkeeping stuff that has to be here. Don't use this stuff, don't refer to it.
*/
#if !defined(_OBJC_UNICHAR_H_)
#define _OBJC_UNICHAR_H_
#endif
#define NS_UNICHAR_IS_EIGHT_BIT 0


@interface NSSimpleCString : NSString {
@package
char *bytes;
int numBytes;
#if __LP64__
int _unused;
#endif
}
@end


@interface NSConstantString : NSSimpleCString
@end


#if __LP64__
#else
extern void *_NSConstantStringClassReference;
#endif

 


 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h       

一、NSString.h文件所在
    1、    IOS  

       $1  /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h
       $2 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator6.1.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h
    2、 mac 
      /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSString.h
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Foundation.framework/Versions/C/Headers/NSString.h

 

二 、NSString.h分析

 

/* NSString.h
Copyright (c) 1994-2012, Apple Inc. All rights reserved.
*/


typedef unsigned
short unichar; 短正值整型,不少于16位


#import <limits.h>
#import <Foundation/NSObject.h> 根类,提供基本运行能力的接口
#import <Foundation/NSRange.h> 截取字符串或者数组
#import <stdarg.h> C标准函数库的头文件,stdarg主要目的是为让函数能够接收不定参数


@class NSData, NSArray, NSDictionary, NSCharacterSet, NSURL, NSError, NSLocale; //@class 告诉编译器,其后面声明的名称是类的名称


FOUNDATION_EXPORT NSString
* const NSParseErrorException; // raised by -propertyList 异常处理处理串定义


#define NSMaximumStringLength (INT_MAX-1) //字符串最大值定义


/* These options apply to the various search/find and comparison methods (except where noted). * 下面部分主要用于搜索,查找,比较
*/
typedef NS_OPTIONS(NSUInteger, NSStringCompareOptions) {
NSCaseInsensitiveSearch
= 1, //不区分大小写比较
NSLiteralSearch = 2, /* Exact character-by-character equivalence */ //区分大小写比较
NSBackwardsSearch = 4, /* Search from end of source string */ //从字符串末尾开始搜索
NSAnchoredSearch = 8, /* Search is limited to start (or end, if NSBackwardsSearch) of source string */ 搜索限制范围的字符串
NSNumericSearch
= 64, /* Added in 10.2; Numbers within strings are compared using numeric value, that is, Foo2.txt < Foo7.txt < Foo25.txt; only applies to compare methods, not find *//按照字符串里的数字为依据,算出顺序
NSDiacriticInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0)
= 128, /* If specified, ignores diacritics (o-umlaut == o) */ //忽略 "-" 符号的比较
NSWidthInsensitiveSearch NS_ENUM_AVAILABLE(10_5, 2_0) = 256, /* If specified, ignores width differences ('a' == UFF41) *//忽略字符串的长度,比较出结果
NSForcedOrderingSearch NS_ENUM_AVAILABLE(10_5, 2_0)
= 512, /* If specified, comparisons are forced to return either NSOrderedAscending or NSOrderedDescending if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with NSCaseInsensitiveSearch specified) */ 忽略不区分大小写比较的选项,并强制返回
NSRegularExpressionSearch NS_ENUM_AVAILABLE(10_7, 3_2)
= 1024 /* Applies to rangeOfString:..., stringByReplacingOccurrencesOfString:..., and replaceOccurrencesOfString:... methods only; the search string is treated as an ICU-compatible regular expression; if set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch */
};


/* Note that in addition to the values explicitly listed below, NSStringEncoding supports encodings provided by CFString.
See CFStringEncodingExt.h for a list of these encodings.
See CFString.h for functions which convert between NSStringEncoding and CFStringEncoding.
*/
enum {
NSASCIIStringEncoding
= 1, /* 0..127 only */
NSNEXTSTEPStringEncoding
= 2,
NSJapaneseEUCStringEncoding
= 3,
NSUTF8StringEncoding
= 4,
NSISOLatin1StringEncoding
= 5,
NSSymbolStringEncoding
= 6,
NSNonLossyASCIIStringEncoding
= 7,
NSShiftJISStringEncoding
= 8, /* kCFStringEncodingDOSJapanese */
NSISOLatin2StringEncoding
= 9,
NSUnicodeStringEncoding
= 10,
NSWindowsCP1251StringEncoding
= 11, /* Cyrillic; same as AdobeStandardCyrillic */
NSWindowsCP1252StringEncoding
= 12, /* WinLatin1 */
NSWindowsCP1253StringEncoding
= 13, /* Greek */
NSWindowsCP1254StringEncoding
= 14, /* Turkish */
NSWindowsCP1250StringEncoding
= 15, /* WinLatin2 */
NSISO2022JPStringEncoding
= 21, /* ISO 2022 Japanese encoding for e-mail */
NSMacOSRomanStringEncoding
= 30,


NSUTF16StringEncoding
= NSUnicodeStringEncoding, /* An alias for NSUnicodeStringEncoding */


NSUTF16BigEndianStringEncoding
= 0x90000100, /* NSUTF16StringEncoding encoding with explicit endianness specified */
NSUTF16LittleEndianStringEncoding
= 0x94000100, /* NSUTF16StringEncoding encoding with explicit endianness specified */


NSUTF32StringEncoding
= 0x8c000100,
NSUTF32BigEndianStringEncoding
= 0x98000100, /* NSUTF32StringEncoding encoding with explicit endianness specified */
NSUTF32LittleEndianStringEncoding
= 0x9c000100 /* NSUTF32StringEncoding encoding with explicit endianness specified */
};
typedef NSUInteger NSStringEncoding;


typedef NS_OPTIONS(NSUInteger, NSStringEncodingConversionOptions) {
NSStringEncodingConversionAllowLossy
= 1,
NSStringEncodingConversionExternalRepresentation
= 2
};


FOUNDATION_EXPORT NSString
* const NSCharacterConversionException;




@interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> 协议


/* NSString primitive (funnel) methods. A minimal subclass of NSString just needs to implement these, although we also recommend getCharacters:range:. See below for the other methods.
*/
- (NSUInteger)length; 返回字符串的长度
- (unichar)characterAtIndex:(NSUInteger)index; 返回在字符串中的某个位置的字符


@end


@interface NSString (NSStringExtensionMethods) 类别


- (void)getCharacters:(unichar *)buffer range:(NSRange)aRange; unichar是两字节长的char,代表unicode的一个字符,截取字符串指定段- (NSString *)substringFromIndex:(NSUInteger)from; 截取指定位置后面的字符串
- (NSString *)substringToIndex:(NSUInteger)to; 截取指定长度的字符串,从索引0开始
- (NSString *)substringWithRange:(NSRange)range; // Hint: Use with rangeOfComposedCharacterSequencesForRange: to avoid breaking up composed characters 截取字符串指定段,返回类型NSString


/* In the compare: methods, the range argument specifies the subrange, rather than the whole, of the receiver to use in the comparison. The range is not applied to the search string. For example, [@"AB" compare:@"ABC" options:0 range:NSMakeRange(0,1)] compares "A" to "ABC", not "A" to "A", and will return NSOrderedAscending.
*/
- (NSComparisonResult)compare:(NSString *)string; 比较字符串
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask; 以某些条件比较字符串
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange; 以某些条件比较字符串至指定段
- (NSComparisonResult)compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)compareRange locale:(id)locale; // locale arg used to be a dictionary pre-Leopard. We now accept NSLocale. Assumes the current locale if non-nil and non-NSLocale. nil continues to mean canonical compare, which doesn't depend on user's locale choice. 以某些限制+本地化(语言环境)比较条件比较字符串至指定段
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;不区别大小比较
- (NSComparisonResult)localizedCompare:(NSString *)string;本地化比较
- (NSComparisonResult)localizedCaseInsensitiveCompare:(NSString *)string; 不区别大小本地化比较


/* localizedStandardCompare:, added in 10.6, should be used whenever file names or other strings are presented in lists and tables where Finder-like sorting is appropriate. The exact behavior of this method may be tweaked in future releases, and will be different under different localizations, so clients should not depend on the exact sorting order of the strings.
*/
- (NSComparisonResult)localizedStandardCompare:(NSString *)string NS_AVAILABLE(10_6, 4_0);

- (BOOL)isEqualToString:(NSString *)aString; 测试两个字符串是否相等
- (BOOL)hasPrefix:(NSString *)aString; 测试字符串是否以 nsstring 开始
- (BOOL)hasSuffix:(NSString *)aString; 测试字符串是否以 nsstring 结尾


/* These methods return length==0 if the target string is not found. So, to check for containment: ([str rangeOfString:@"target"].length > 0). Note that the length of the range returned by these methods might be different than the length of the target string, due composed characters and such.
*/
typedef
struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
- (NSRange)rangeOfString:(NSString *)aString; 搜索字符串One是否存在于字符串Two
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask; 以某些限制条件搜索字符串One是否存在于字符串Two
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件搜索字符串One是否存在于字符串Two指定位置段
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)searchRange locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0);以某些限制条件+本地化限制搜索字符串One是否存在于字符串Two指定位置段


/* These return the range of the first character from the set in the string, not the range of a sequence of characters.
*/
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet; 指定字符集搜索
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask;以某些限制条件指定字符集搜索
- (NSRange)rangeOfCharacterFromSet:(NSCharacterSet *)aSet options:(NSStringCompareOptions)mask range:(NSRange)searchRange; 以某些限制条件指定字符集搜索指定位置段


- (NSRange)rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index; 以字符串的字符编码指定索引查找位置
- (NSRange)rangeOfComposedCharacterSequencesForRange:(NSRange)range NS_AVAILABLE(10_5, 2_0); 以字符串的字符编码指定区域段查找位置


- (NSString *)stringByAppendingString:(NSString *)aString; 将字符串One添加字符串Two后面
- (NSString *)stringByAppendingFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 将多个字符串添加字符串Two后面


/* The following convenience methods all skip initial space characters (whitespaceSet) and ignore trailing characters. NSScanner can be used for more "exact" parsing of numbers.
*/
- (double)doubleValue; 返回转化的double类型
- (float)floatValue; 返回转化的float类型
- (int)intValue; 返回转化的int类型
- (NSInteger)integerValue NS_AVAILABLE(10_5, 2_0); 返回转化的NSInteger(32位系统NSInteger是一个int,即32位,但当时64位系统时,NSInteger便是64位的)类型
- (long long)longLongValue NS_AVAILABLE(10_5, 2_0); 返回转化的 长int类型
- (BOOL)boolValue NS_AVAILABLE(10_5, 2_0); // Skips initial space characters (whitespaceSet), or optional -/+ sign followed by zeroes. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9. It ignores any trailing characters. 返回转化的 长BOOL类型


- (NSArray *)componentsSeparatedByString:(NSString *)separator; 字符串转化为数组
- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator NS_AVAILABLE(10_5, 2_0); 依据字符编码,分割字符串


- (NSString *)commonPrefixWithString:(NSString *)aString options:(NSStringCompareOptions)mask;


/* The following three case methods perform the canonical (non-localized) mappings. They are suitable for programming operations that require stable results not depending on the user's locale preference. For localized case mapping for strings presented to users, use their corresponding methods with locale argument below.
*/
- (NSString *)uppercaseString; 所有字符转化为大写
- (NSString *)lowercaseString; 所有字符转化为小写
- (NSString *)capitalizedString; 所有单词首字母转化大写


/* The following methods perform localized case mappings based on the locale specified. Passing nil indicates the canonical mapping. For the user preference locale setting, specify +[NSLocale currentLocale].
*/
- (NSString *)uppercaseStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0); 所有字符转化为大写(本地化)
- (NSString *)lowercaseStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0);所有字符转化为小写(本地化)
- (NSString *)capitalizedStringWithLocale:(NSLocale *)locale NS_AVAILABLE(10_8, 6_0); 所有单词首字母转化大写(本地化)


- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set; 字符串替换
- (NSString *)stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex;将字符串two指定索引段添加指定索引段字符串One

- (void)getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range; 指定段分行去字符串
- (NSRange)lineRangeForRange:(NSRange)range; 返回字符串指定段的位置和长度


- (void)getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;指定段分段取字符串
- (NSRange)paragraphRangeForRange:(NSRange)range; 指定段分段的位置和长度

#if NS_BLOCKS_AVAILABLE
typedef NS_OPTIONS(NSUInteger, NSStringEnumerationOptions) {
// Pass in one of the "By" options:
NSStringEnumerationByLines = 0, // Equivalent to lineRangeForRange:
NSStringEnumerationByParagraphs = 1, // Equivalent to paragraphRangeForRange:
NSStringEnumerationByComposedCharacterSequences = 2, // Equivalent to rangeOfComposedCharacterSequencesForRange:
NSStringEnumerationByWords = 3,
NSStringEnumerationBySentences
= 4,
// ...and combine any of the desired additional options:
NSStringEnumerationReverse = 1UL << 8,
NSStringEnumerationSubstringNotRequired
= 1UL << 9,
NSStringEnumerationLocalized
= 1UL << 10 // User's default locale
};


/* In the enumerate methods, the blocks will be invoked inside an autorelease pool, so any values assigned inside the block should be retained.
*/
- (void)enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 检查是否在指定范围内是否有匹配的字符串
- (void)enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block NS_AVAILABLE(10_6, 4_0); 枚举字符串所有行
#endif


- (NSString *)description; 返回字符串


- (NSUInteger)hash; 返回字符串哈希地址


/*** Encoding methods ***/


- (NSStringEncoding)fastestEncoding; // Result in O(1) time; a rough estimate 字符串最快编码值
- (NSStringEncoding)smallestEncoding; // Result in O(n) time; the encoding in which the string is most compact 字符串最小编码值


- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy; // External representation 返回指定编码的NSData对象
- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding; // External representation 返回指定编码的NSData对象


- (BOOL)canBeConvertedToEncoding:(NSStringEncoding)encoding; 判断是否可以无损转化编码


/* Methods to convert NSString to a NULL-terminated cString using the specified encoding. Note, these are the "new" cString methods, and are not deprecated like the older cString methods which do not take encoding arguments.
*/
- (__strong const char *)cStringUsingEncoding:(NSStringEncoding)encoding NS_RETURNS_INNER_POINTER; // "Autoreleased"; NULL return if encoding conversion not possible; for performance reasons, lifetime of this should not be considered longer than the lifetime of the receiving string (if the receiver string is freed, this might go invalid then, before the end of the autorelease scope) char编码转换
- (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding; // NO return if conversion not possible due to encoding errors or too small of a buffer. The buffer should include room for maxBufferCount bytes; this number should accomodate the expected size of the return value plus the NULL termination character, which this method adds. (So note that the maxLength passed to this method is one more than the one you would have passed to the deprecated getCString:maxLength:.) c字符串编码转化是否成功


/* Use this to convert string section at a time into a fixed-size buffer, without any allocations. Does not NULL-terminate.
buffer is the buffer to write to; if NULL, this method can be used to computed size of needed buffer.
maxBufferCount is the length of the buffer in bytes. It's a good idea to make sure this is at least enough to hold one character's worth of conversion.
usedBufferCount is the length of the buffer used up by the current conversion. Can be NULL.
encoding is the encoding to convert to.
options specifies the options to apply.
range is the range to convert.
leftOver is the remaining range. Can be NULL.
YES return indicates some characters were converted. Conversion might usually stop when the buffer fills,
but it might also stop when the conversion isn't possible due to the chosen encoding.
*/
- (BOOL)getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger *)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover; 指定缓存区转换


/* These return the maximum and exact number of bytes needed to store the receiver in the specified encoding in non-external representation. The first one is O(1), while the second one is O(n). These do not include space for a terminating null.
*/
- (NSUInteger)maximumLengthOfBytesUsingEncoding:(NSStringEncoding)enc; // Result in O(1) time; the estimate may be way over what's needed. Returns 0 on error (overflow) 字符串编码时需要用的字节长度
- (NSUInteger)lengthOfBytesUsingEncoding:(NSStringEncoding)enc; // Result in O(n) time; the result is exact. Returns 0 on error (cannot convert to specified encoding, or overflow)字符串编码时需要用到最大字节长度

FormC 指示使用完全规范化分解对 Unicode 字符串进行规范化,然后将序列替换为其原复合字符(如果可能)。FormD 指示使用完全规范化分解对 Unicode 字符串进行规范化。FormKC 指示使用完全兼容性分解对 Unicode 字符串进行规范化,然后将序列替换为其原复合字符(如果可能)。FormKD 指示使用完全兼容性分解对 Unicode 字符串进行规范化。
- (NSString *)decomposedStringWithCanonicalMapping; Unicode范式D标准化
- (NSString *)precomposedStringWithCanonicalMapping; Unicode范式C标准化
- (NSString *)decomposedStringWithCompatibilityMapping; Unicode范式KD标准化
- (NSString *)precomposedStringWithCompatibilityMapping; Unicode范式KC标准化


/* Returns a string with the character folding options applied. theOptions is a mask of compare flags with *InsensitiveSearch suffix.
*/
- (NSString *)stringByFoldingWithOptions:(NSStringCompareOptions)options locale:(NSLocale *)locale NS_AVAILABLE(10_5, 2_0); 本地化字符串折叠


/* Replace all occurrences of the target string in the specified range with replacement. Specified compare options are used for matching target. If NSRegularExpressionSearch is specified, the replacement is treated as a template, as in the corresponding NSRegularExpression methods, and no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch.
*/
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange NS_AVAILABLE(10_5, 2_0); 字符串指定区域段替换


/* Replace all occurrences of the target string with replacement. Invokes the above method with 0 options and range of the whole string.
*/
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0); 字符串替换


/* Replace characters in range with the specified string, returning new string.
*/
- (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement NS_AVAILABLE(10_5, 2_0); 指定区域段字符串替换


- (__strong const char *)UTF8String NS_RETURNS_INNER_POINTER; // Convenience to return null-terminated UTF8 representation 转化为char


/* User-dependent encoding who value is derived from user's default language and potentially other factors. The use of this encoding might sometimes be needed when interpreting user documents with unknown encodings, in the absence of other hints. This encoding should be used rarely, if at all. Note that some potential values here might result in unexpected encoding conversions of even fairly straightforward NSString content --- for instance, punctuation characters with a bidirectional encoding.
*/
+ (NSStringEncoding)defaultCStringEncoding; // Should be rarely used 默认C字符串编码


+ (const NSStringEncoding *)availableStringEncodings; 当前编码值
+ (NSString *)localizedNameOfStringEncoding:(NSStringEncoding)encoding; 编码的名称


/*** Creation methods ***/


/* In general creation methods in NSString do not apply to subclassers, as subclassers are assumed to provide their own init methods which create the string in the way the subclass wishes. Designated initializers of NSString are thus init and initWithCoder:.
*/
- (id)init; 初始化NSSting 对象
- (id)initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer; /* "NoCopy" is a hint */ 指定缓冲区,编码和字节长度初始化NSSting 对象
- (id)initWithCharacters:(const unichar *)characters length:(NSUInteger)length; 指定unichar字符,字节长度初始化NSSting 对象
- (id)initWithUTF8String:(const char *)nullTerminatedCString; char 转化为NSString对象
- (id)initWithString:(NSString *)aString; 指定字符串初始化NSSting 对象
- (id)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);格式化多个字符串初始化NSSting 对象
- (id)initWithFormat:(NSString *)format arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0); 格式化字符串初始化NSSting 对象
- (id)initWithFormat:(NSString *)format locale:(id)locale, ... NS_FORMAT_FUNCTION(1,3);本地化格式化多个字符串初始化NSSting 对象
- (id)initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);本地化格式化字符串初始化NSSting 对象
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding; 指定编码转化NSData数据
- (id)initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding; 指定编码和字节数初始化NSString对象
- (id)initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer;指定缓冲区,编码和字节数初始化NSString对象


+ (id)string; 初始化空string对象
+ (id)stringWithString:(NSString *)string;初始化string对象
+ (id)stringWithCharacters:(const unichar *)characters length:(NSUInteger)length; 返回指定长度unichar的C字符串
+ (id)stringWithUTF8String:(const char *)nullTerminatedCString; 转化C字符串为UTF8串
+ (id)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 格式化出初始化NSString对象,自动释放内存
+ (id)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 本地化格式化出初始化NSString对象,自动释放内存


- (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding; 指定编码初始化C字符串,需要手动释放内存
+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc; 指定编码初始化C字符串,自动释放内存


/* These use the specified encoding. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).
*/
- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取URL地址数据转换为字符串,需要手动释放内存(已知编码)
- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取FILE地址数据转换为字符串,需要手动释放内存(已知编码)
+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码读取URL地址数据转换为字符串,自动释放内存(已知编码)
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,自动释放内存(已知编码)


/* These try to determine the encoding, and return the encoding which was used. Note that these methods might get "smarter" in subsequent releases of the system, and use additional techniques for recognizing encodings. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors).
*/
- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取URL地址数据转换为字符串,需要手动释放内存(未知编码)
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,需要手动释放内存(未知编码)
+ (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取URL地址数据转换为字符串,自动释放内存(未知编码)
+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error;指定编码读取FILE地址数据转换为字符串,自动释放内存(未知编码)


/* Write to specified url or path using the specified encoding. The optional error return is to indicate file system or encoding errors.
*/
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码把数据写入文件
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error; 指定编码把数据写入文件


@end




@interface NSMutableString : NSString


/* NSMutableString primitive (funnel) method. See below for the other mutation methods.
*/
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString; 使用 aString 替换 range 指定的区域段


@end


@interface NSMutableString (NSMutableStringExtensionMethods)


- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc; 以索引loc 为起始位置插入 aString
- (void)deleteCharactersInRange:(NSRange)range; 删除指定区域段字符
- (void)appendString:(NSString *)aString; 追加字符串aString
- (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); 追加多个字符串(格式化)
- (void)setString:(NSString *)aString; 设置新的 aString


/* In addition to these two, NSMutableString responds properly to all NSString creation methods.
*/
- (id)initWithCapacity:(NSUInteger)capacity; 初始化一个容量为capacity的字符串,需要手动释放内存
+ (id)stringWithCapacity:(NSUInteger)capacity;初始化一个容量为capacity的字符串,自动释放内存( 暂且分配指定容量,用于内存优化,可以大于设置的容量)


/* This method replaces all occurrences of the target string with the replacement string, in the specified range of the receiver string, and returns the number of replacements. NSBackwardsSearch means the search is done from the end of the range (the results could be different); NSAnchoredSearch means only anchored (but potentially multiple) instances will be replaced. NSLiteralSearch and NSCaseInsensitiveSearch also apply. NSNumericSearch is ignored. Use NSMakeRange(0, [receiver length]) to process whole string. If NSRegularExpressionSearch is specified, the replacement is treated as a template, as in the corresponding NSRegularExpression methods, and no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch.
*/
- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
指定限制条件(大小忽略等)指定区段中的replacement替换成target

@end






@interface NSString (NSExtendedStringPropertyListParsing)


/* These methods are no longer recommended since they do not work with property lists and strings files in binary plist format. Please use the APIs in NSPropertyList.h instead.
*/
- (id)propertyList;字符串转化为属性列表
- (NSDictionary *)propertyListFromStringsFileFormat;字符串转化为字典


@end






@interface NSString (NSStringDeprecated)


/* The following methods are deprecated and will be removed from this header file in the near future. These methods use [NSString defaultCStringEncoding] as the encoding to convert to, which means the results depend on the user's language and potentially other settings. This might be appropriate in some cases, but often these methods are misused, resulting in issues when running in languages other then English. UTF8String in general is a much better choice when converting arbitrary NSStrings into 8-bit representations. Additional potential replacement methods are being introduced in NSString as appropriate.
*/
- (const char *)cString NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (const char *)lossyCString NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (NSUInteger)cStringLength NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (void)getCString:(char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (void)getCString:(char *)bytes maxLength:(NSUInteger)maxLength NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (void)getCString:(char *)bytes maxLength:(NSUInteger)maxLength range:(NSRange)aRange remainingRange:(NSRangePointer)leftoverRange NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);


- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);


- (id)initWithContentsOfFile:(NSString *)path NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (id)initWithContentsOfURL:(NSURL *)url NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
+ (id)stringWithContentsOfFile:(NSString *)path NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
+ (id)stringWithContentsOfURL:(NSURL *)url NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);


- (id)initWithCStringNoCopy:(char *)bytes length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (id)initWithCString:(const char *)bytes length:(NSUInteger)length NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
- (id)initWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
+ (id)stringWithCString:(const char *)bytes length:(NSUInteger)length NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);
+ (id)stringWithCString:(const char *)bytes NS_DEPRECATED(10_0, 10_4, 2_0, 2_0);


/* This method is unsafe because it could potentially cause buffer overruns. You should use -getCharacters:range: instead.
*/
- (void)getCharacters:(unichar *)buffer;


@end


enum {
NSProprietaryStringEncoding
= 65536 /* Installation-specific encoding */
};






/* The rest of this file is bookkeeping stuff that has to be here. Don't use this stuff, don't refer to it.
*/
#if !defined(_OBJC_UNICHAR_H_)
#define _OBJC_UNICHAR_H_
#endif
#define NS_UNICHAR_IS_EIGHT_BIT 0


@interface NSSimpleCString : NSString {
@package
char *bytes;
int numBytes;
#if __LP64__
int _unused;
#endif
}
@end


@interface NSConstantString : NSSimpleCString
@end


#if __LP64__
#else
extern void *_NSConstantStringClassReference;
#endif

 


 /Appl




你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: