我来自JS,所以不确定字符串字面量。
我有以下语法
[GMSServices provideAPIKey:@"ABCXYZ"];
if I remove @ from the above, swift throws the following error String literal must be prefixed by '@'
Can someone explain me why I am getting this error and what are string literals? This answer wasn't helpful in above context: What does the @ prefix do on string literals in C#
首先,不要在Internet上发布API密钥。我已经编辑了您的问题以删除密钥,但是它在问题历史记录中仍然可见。
Anyway, the code you quoted is Objective-C code, not Swift code. That code declares an
NSString
literal.NSString
is an Objective-C class. In contrast to a plain C string, theNSString
class provides many APIs for using and manipulating strings, and the Objective-C runtime managesNSString
memory so you don't have to.在Swift中,等效代码为
Here, we are using a Swift
String
literal, which is similar toNSString
, with many APIs and with automatic memory management, but we don't need an@
prefix because Swift is not a superset of C.