015-CFBridgingRelease简析

上一篇:014-移动支付12-SDK设计-整体结构原理与设计

一、CFBridgingRelease

Moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC.

把非OC的指针指向OC并且转换成ARC。

二、Declaration

id CFBridgingRelease(CFTypeRef X);

三、Discussion

You use this function to cast a Core Foundation-style object as an Objective-C object and transfer ownership of the object to ARC such that you don’t have to release the object, as illustrated in this example:

使用举例如下:

CFStringRef cfName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *name = (NSString *)CFBridgingRelease(cfName);

四、相关CFBridgingRetain

Casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to the caller.

使用举例:

NSString *string = <#Get a string#>;
CFStringRef cfString = (CFStringRef)CFBridgingRetain(string);
// Use the CF string.
CFRelease(cfString);

五、实战使用

字符串分类,让字符串转url

/**
 字符串转url

 @return 由字符串转化成的NSURL
 */
- (NSURL *)url;
/**
 字符串转url
 
 @return 由字符串转化成的NSURL
 */
- (NSURL *)url {
    return [NSURL URLWithString:(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL, kCFStringEncodingUTF8))];
}

六、附CFURLCreateStringByAddingPercentEscapes

Creates a copy of a string, replacing certain characters with the equivalent percent escape sequence based on the specified encoding.

创建一个字符串副本,代替并使用指定格式编码。

6.1 Declaration

定义:

CFStringRef CFURLCreateStringByAddingPercentEscapes(CFAllocatorRef allocator, CFStringRef originalString, CFStringRef charactersToLeaveUnescaped, CFStringRef legalURLCharactersToBeEscaped, CFStringEncoding encoding);

6.2 参数

  • allocator
The allocator to use to allocate memory for the new CFString
 object. Pass NULL
 or kCFAllocatorDefault to use the current default allocator.

这个参数传NULL和kCFAllocatorDefault就行

  • originalString
The CFString object to copy.

传一个CFString对象

  • charactersToLeaveUnescaped
Characters whose percent escape sequences you want to leave intact. Pass NULL to specify that all illegal characters be escaped.

字符转义,传NULL指定所有非法字符转义。
如例所示,里面的字符在url中是非法的,需要转义。

  • legalURLCharactersToBeEscaped
Legal characters to be escaped. Pass NULL to specify that no legal characters be replaced.

合法字符除外。传NULL指定不合法字符被替换。

  • encoding
    编码格式,毫无疑问的UTF8。

6.3 返回值

A copy of originalString replacing certain characters.

一份原始字符串取代了某些字符。

6.4 例子里使用CFURLCreateStringByAddingPercentEscapes刚好合适

下一篇:016-设计模式一:策略模式(Strategy)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,386评论 19 139
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 8,455评论 0 4
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,929评论 18 399
  • 晚上睡前,小朋友突然问我,妈妈你会老么?像太姥姥那样? 是的,我们都会老,都会有像太姥姥那样走不动,听不见的时候。...
    i_Chantelle阅读 1,677评论 0 0
  • 我不知道你是否和我一样,每周末的晚上会格外焦虑,难以入眠,准确的来说应该是舍不得去睡觉。一周七天,只有周末的两天是...
    爱做梦的白羊喵阅读 5,307评论 5 16