NSString简单细说(一)—— NSString整体架构

前言

NSString虽然我们每天都用也很简单,这里我就总结一下,做到NSString简单细说深说,这里首先要对整个NSString有整体的感知和认识,详细的以后会续写,不对的地方大家批评补充。

苹果开发文档


NSString功能架构

下面我们开始,首先看一下NSString有哪些功能相对应的属性和类。

功能 方法名
简单实例化 1. + (instancetype) string;
2. - (instancetype) init
3. - (instancetype) initWithBytes:(const void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;
4. - (instancetype) initWithBytesNoCopy:(void *)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer;
5. - (instancetype) initWithCharacters:(const unichar *)characters length:(NSUInteger)length;
6. - (instancetype) initWithCharactersNoCopy:(unichar *)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer;
7. - (instancetype) initWithString:(NSString *)aString;
8. - (instancetype) initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
9. - (instancetype) initWithUTF8String:(const char *)nullTerminatedCString;
10. - (instancetype) initWithFormat:(NSString *)format, ...;
11. - (instancetype) initWithFormat:(NSString *)format arguments:(va_list)argList;
12. - (instancetype) initWithFormat:(NSString *)format locale:(id)locale, ...;
13. - (instancetype) initWithFormat:(NSString *)format locale:(id)locale arguments:(va_list)argList;
14. - (instancetype) initWithData:(NSData *)data encoding:(NSStringEncoding)encoding;
15. + (instancetype) stringWithFormat:(NSString *)format, ...;
16. + (instancetype) localizedStringWithFormat:(NSString *)format, ...;
17. + (NSString ) localizedUserNotificationStringForKey:(NSString)key arguments:(NSArray*)arguments;
18. + (instancetype) stringWithCharacters:(const unichar *)characters length:(NSUInteger)length;
19. + (instancetype) stringWithString:(NSString *)string;
20. + (instancetype) stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;
21. + (instancetype) stringWithUTF8String:(const char *)nullTerminatedCString;
文件实例化 1. + (instancetype) stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
2. - (instancetype) initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
3. + (instancetype) stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
4. - (instancetype) initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
URL实例化 1. + (instancetype) stringWithContentsOfURL:(NSURL*)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
2. - (instancetype) initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
3. + (instancetype) stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
4. - (instancetype) initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError * _Nullable *)error;
写向文件和URL 1. - (BOOL) writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
2. - (BOOL) writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError * _Nullable *)error;
获取字符串长度 1. @property(readonly) NSUInteger length;
2. - (NSUInteger) lengthOfBytesUsingEncoding:(NSStringEncoding)enc;
3. - (NSUInteger) maximumLengthOfBytesUsingEncoding:(NSStringEncoding)enc;
获取字符字节 1. - (unichar) characterAtIndex:(NSUInteger)index;
2. - (void) getCharacters:(unichar *)buffer range:(NSRange)range;
3. - (BOOL) getBytes:(void *)buffer maxLength:(NSUInteger)maxBufferCount usedLength:(NSUInteger *)usedBufferCount encoding:(NSStringEncoding)encoding options:(NSStringEncodingConversionOptions)options range:(NSRange)range remainingRange:(NSRangePointer)leftover;
获取C字符串 1. - (const char *) cStringUsingEncoding:(NSStringEncoding)encoding;
2. - (BOOL) getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding;
3. @property(readonly) const char *UTF8String;
字符串比较 1. - (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;
2. - (NSComparisonResult) localizedCaseInsensitiveCompare:(NSString *)string;
3. - (NSComparisonResult) compare:(NSString *)string;
4. - (NSComparisonResult) localizedCompare:(NSString *)string;
5. - (NSComparisonResult) compare:(NSString *)string options:(NSStringCompareOptions)mask;
6. - (NSComparisonResult) compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare;
7. - (NSComparisonResult) compare:(NSString *)string options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToCompare locale:(id)locale;
8. - (NSComparisonResult) localizedStandardCompare:(NSString *)string;
9. - (BOOL) hasPrefix:(NSString *)str;
10. - (BOOL) hasSuffix:(NSString *)str;
11. - (BOOL) isEqualToString:(NSString *)aString;
12. @property(readonly) NSUInteger hash;
字符串的合并 1. - (NSString *) stringByAppendingFormat:(NSString *)format, ...;
2. - (NSString *)stringByAppendingString:(NSString *)aString;
3. - (NSString *) stringByPaddingToLength:(NSUInteger)newLength withString:(NSString *)padString startingAtIndex:(NSUInteger)padIndex;
字符串的分割 1. - (NSArray<NSString *> *) componentsSeparatedByString:(NSString *)separator;
2. - (NSArray<NSString *> *) componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator;
3. - (NSString *) stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
4. - (NSString *) substringFromIndex:(NSUInteger)from;
5. - (NSString *) substringWithRange:(NSRange)range;
6. - (NSString *) substringToIndex:(NSUInteger)to;
字符串的查询 1. - (BOOL) containsString:(NSString *)str;
2. - (BOOL) localizedCaseInsensitiveContainsString:(NSString *)str;
3. - (BOOL) localizedStandardContainsString:(NSString *)str;
4. - (NSRange) rangeOfCharacterFromSet:(NSCharacterSet *)searchSet;
5. - (NSRange) rangeOfCharacterFromSet:(NSCharacterSet *) searchSet options:(NSStringCompareOptions)mask;
6. - (NSRange) rangeOfCharacterFromSet:(NSCharacterSet *)searchSet options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch;
7. - (NSRange) rangeOfString:(NSString *)searchString;
8. - (NSRange) rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;
9. - (NSRange) rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch;
10. - (NSRange) rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask range:(NSRange)rangeOfReceiverToSearch locale:(NSLocale *)locale;
11. - (NSRange) localizedStandardRangeOfString:(NSString *)str;
12. - (void) enumerateLinesUsingBlock:(void (^)(NSString *line, BOOL *stop))block;
13. - (void) enumerateSubstringsInRange:(NSRange)range options:(NSStringEnumerationOptions)opts usingBlock:(void (^)(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop))block;
子字符串替换 1. - (NSString *) stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement;
2. - (NSString *) stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;
3. - (NSString *) stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement;
线和段落区间 1. - (void) getLineStart:(NSUInteger *)startPtr end:(NSUInteger *)lineEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;
2. - (NSRange) lineRangeForRange:(NSRange)range;
3. - (void) getParagraphStart:(NSUInteger *)startPtr end:(NSUInteger *)parEndPtr contentsEnd:(NSUInteger *)contentsEndPtr forRange:(NSRange)range;
4. - (NSRange) paragraphRangeForRange:(NSRange)range;
返回给定区间 1. - (NSRange) rangeOfComposedCharacterSequenceAtIndex:(NSUInteger)index;
2. - (NSRange) rangeOfComposedCharacterSequencesForRange:(NSRange)range;
转化属性列表 1. - (id) propertyList;
2. - (NSDictionary *) propertyListFromStringsFileFormat;
画字符串 1. - (void) drawAtPoint:(CGPoint)point withAttributes:(NSDictionary<NSString *,id> *)attrs;
2. - (void) drawInRect:(CGRect)rect withAttributes:(NSDictionary<NSString *,id> *)attrs;
3. - (void) drawWithRect:(CGRect)rect options:(NSStringDrawingOptions)options attributes:(NSDictionary<NSString *,id> *)attributes context:(NSStringDrawingContext *)context;
4. - (CGRect) boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary<NSString *,id> *)attributes context:(NSStringDrawingContext *)context;
5. - (CGSize) sizeWithAttributes:(NSDictionary<NSString *,id> *)attrs;
折叠 1. - (NSString *) stringByFoldingWithOptions:(NSStringCompareOptions)options locale:(NSLocale *)locale;
字符串前缀分享 1. - (NSString *) commonPrefixWithString:(NSString *)str options:(NSStringCompareOptions)mask;
形改变 1. @property(readonly, copy) NSString *lowercaseString;
2. @property(readonly, copy) NSString *localizedLowercaseString;
3. - (NSString *)lowercaseStringWithLocale:(NSLocale *)locale;
4. @property(readonly, copy) NSString *uppercaseString;
5. @property(readonly, copy) NSString *localizedUppercaseString;
6. - (NSString *) uppercaseStringWithLocale:(NSLocale *)locale;
7. @property(readonly, copy) NSString *capitalizedString;
8. @property(readonly, copy) NSString *localizedCapitalizedString;
9. - (NSString *) capitalizedStringWithLocale:(NSLocale *)locale;
映射 1. @property(readonly, copy) NSString *decomposedStringWithCanonicalMapping;
2. @property(readonly, copy) NSString *decomposedStringWithCompatibilityMapping;
3. @property(readonly, copy) NSString *precomposedStringWithCanonicalMapping;
4. @property(readonly, copy) NSString *precomposedStringWithCompatibilityMapping;
获取数值 1. @property(readonly) double doubleValue;
2. @property(readonly) float floatValue;
3. @property(readonly) int intValue;
4. @property(readonly) NSInteger integerValue;
5. @property(readonly) long long longLongValue;
6. @property(readonly) BOOL boolValue;
编码 1. + (NSStringEncoding) stringEncodingForData:(NSData *)data encodingOptions:(NSDictionary<NSStringEncodingDetectionOptionsKey,id> *)opts convertedString:(NSString * _Nullable *)string usedLossyConversion:(BOOL *)usedLossyConversion;
2. + (NSString *) localizedNameOfStringEncoding:(NSStringEncoding)encoding;
3. - (BOOL) canBeConvertedToEncoding:(NSStringEncoding)encoding;
4. - (NSData *) dataUsingEncoding:(NSStringEncoding)encoding;
5. - (NSData *) dataUsingEncoding:(NSStringEncoding)encoding allowLossyConversion:(BOOL)lossy;
6. @property(readonly, copy) NSString *description;
7. @property(readonly) NSStringEncoding fastestEncoding;
8. @property(readonly) NSStringEncoding smallestEncoding;
路经相关 1. + (NSString *) pathWithComponents:(NSArray<NSString *> *)components;
2. @property(readonly, copy) NSArray<NSString *> *pathComponents;
3. - (NSUInteger) completePathIntoString:(NSString * _Nullable *)outputName caseSensitive:(BOOL)flag matchesIntoArray:(NSArray<NSString *> * _Nullable *)outputArray filterTypes:(NSArray<NSString *> *)filterTypes;
4. @property(readonly) const char *fileSystemRepresentation;
5. - (BOOL) getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max;
6. @property(getter=isAbsolutePath, readonly) BOOL absolutePath;
7. @property(readonly, copy) NSString *lastPathComponent;
8. @property(readonly, copy) NSString *pathExtension;
9. @property(readonly, copy) NSString *stringByAbbreviatingWithTildeInPath;
10. - (NSString *) stringByAppendingPathComponent:(NSString *)str;
11.- (NSString *) stringByAppendingPathExtension:(NSString *)str;
12.@property(readonly, copy) NSString *stringByDeletingLastPathComponent;
13.@property(readonly, copy) NSString *stringByDeletingPathExtension;
14.@property(readonly, copy) NSString *stringByExpandingTildeInPath;
15.@property(readonly, copy) NSString *stringByResolvingSymlinksInPath;
16.@property(readonly, copy) NSString *stringByStandardizingPath;
17.- (NSArray<NSString *> *)stringsByAppendingPaths:(NSArray<NSString *> *)paths;
URL相关 1.- (NSString *) stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters;
2.@property(readonly, copy) NSString *stringByRemovingPercentEncoding;
语言标记 1.- (void) enumerateLinguisticTagsInRange:(NSRange)range scheme:(NSString *)tagScheme options:(NSLinguisticTaggerOptions)opts orthography:(NSOrthography *)orthography usingBlock:(void (^)(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop))block;
2.- (NSArray<NSString *> *) linguisticTagsInRange:(NSRange)range scheme:(NSString *)tagScheme options:(NSLinguisticTaggerOptions)opts orthography:(NSOrthography *)orthography tokenRanges:(NSArray<NSValue *> * _Nullable *)tokenRanges;

看到这里可能大家晕了,还得吐槽下简书的markdown有的*都不显示,有时候显示。这些类属性或者方法,可以参考下图。

API-NSString-1
API-NSString-2
API-NSString-3
API-NSString-4
API-NSString-5
API-NSString-6
API-NSString-7
API-NSString-8
API-NSString-9
API-NSString-10

致谢

谢谢大家对我的持续关注。上面只是一个主要的框架展示,主要是让大家对这个框架有一个大体的认识,后面的会细说每个方法和属性的使用。

未完,待续~~~~,累了,休息了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,265评论 6 490
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,078评论 2 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 156,852评论 0 347
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,408评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,445评论 5 384
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,772评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,921评论 3 406
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,688评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,130评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,467评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,617评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,276评论 4 329
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,882评论 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,740评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,967评论 1 265
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,315评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,486评论 2 348

推荐阅读更多精彩内容