cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __C
2017-05-20 10:58
831 查看
第一步:
第二步:
http://stackoverflow.com/questions/6725558/cast-of-objective-c-pointer-type-nsstring-to-c-pointer-type-cfstringref-a
activeoldestvotes
第二步:
http://stackoverflow.com/questions/6725558/cast-of-objective-c-pointer-type-nsstring-to-c-pointer-type-cfstringref-a
When converting an Objective-C program to a Objective-C ARC, I get the error:"cast of Objective-C pointer type 'NSString *' to C pointer type 'CFStringRef' (aka 'const struct __CFString *') requires a bridged cast " The code is as follows: - (NSString *)_encodeString:(NSString *)string { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)string, // this is line in error NULL, (CFStringRef)@";/?:@&=$+{}<>,", kCFStringEncodingUTF8); return [result autorelease]; } What is a bridged cast? objective-c casting automatic-ref-counting
| |||||
a comment |
3 Answers
activeoldestvotesup vote56down voteaccepted | Have a look at the ARC documentation on the LLVM website. You'll have to use __bridgeor one of the other keywords. This is because Core Foundation objects (CF*Refs) are not controlled by ARC, only Obj-C objects are. So when you convert between them, you have to tell ARC about the object's ownership so it can properly clean them up. The simplest case is a __bridgecast, for which ARC will not do any extra work (it assumes you handle the object's memory yourself).
| ||||
a comment |
up vote18down vote | Here is a nice ARC tutorial that I found to be easier to understand than Apple's documentation that @jtbandes references. Take a look at the section titled "Toll free bridging" in particular.
| ||||||||
a comment |
up vote0down vote | I know this is an old thread, I came across this issue while I need to use - (NSString *)URLEncodedString { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)self, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8); [result autorelease]; return result; } So what I did is go to Target > Build phase > Compile sources. There is your file listed, double click on that and add following lines next to your file. -fno-objc-arc |
相关文章推荐
- Implicit conversion of an Objective-C pointer to '__autoreleasing instancetype *' (aka '__autoreleas
- 【问题处理】Unable to cast object of type 'System.DBNull' to type 'System.String'.
- ios implicit conversion of an objective-c pointer to 'NSString *__autoreleasing *' is disallowed wit
- C++编译出现binding ‘const string {aka const std::__cxx11::basic_string<char>}’ to reference of type ‘std
- org.hibernate.type.StringType cannot be cast to org.hibernate.type.VersionType
- Err:The "." operator was supplied with an index value of type "java.lang.String" to be applied to a List or array
- Failed to convert property value of type [java.lang.String] to required type [boolean] for property
- Value of type java.lang.String cannot be converted to JSONObject
- Sending 'ccColor4B' (aka 'struct _ccColor4B') to parameter of incompatible type 'CIColor *'错误
- Sending 'ccColor4B' (aka 'struct _ccColor4B') to parameter of incompatible type 'CIColor *'
- 解决方法:Unable to cast COM object of type 'Shell32.ShellClass' to interface 'Shell32.IShellDispatch5'
- deletion of pointer to incomplete type 'A'; no destructor called
- Object of type 'String' cannot be converted to type 'Function'
- Value of type java.lang.String cannot be converted to JSONObject
- org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArra
- Compiler Warning C4150: deletion of pointer to incomplete type 'XXX'; no destructor called
- Question 31: A C++ developer wants to handle a static_cast() operation for the class String shown below. Which of the following
- jsf配置问题 Error performing conversion of value ' of type class java.lang.String to type class
- Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching e
- Unable to cast COM object of type 'Word.ApplicationClass' to interface type 'Word._Application'"