最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Swift代码出错:Postfix ‘=’ is reserved

Swift crifan 2137浏览 0评论

swift代码:

private let JoinMessage_joinerId    = "JoinMessage_joinerId"
private let JoinMessage_joinerName  = "JoinMessage_joinerName"
private let JoinMessage_operatorId  = "JoinMessage_operatorId"
private let JoinMessage_operatorName= "JoinMessage_operatorName"
private let JoinMessage_objectId    = "JoinMessage_objectId"
private let JoinMessage_objectName  = "JoinMessage_objectName"

出错:

Postfix ‘=’ is reserved

如图:

此处,已经知道,改为:

private let JoinMessage_operatorName = "JoinMessage_operatorName"

即可。但是要搞清楚原因

搜:

swift Postfix ‘=’ is reserved

Swift: prefix/postfix ‘=’ is reserved | Jason De Arte

Operators are made up of one or more of the following characters: /, =, -, +, !, *, %, <, >, &, |, ^, ~, and .. That said, the tokens =, ->, //, /*, */, ., and the unary prefix operator & are reserved. These tokens can’t be overloaded, nor can they be used to define custom operators.

The whitespace around an operator is used to determine whether an operator is used as a prefix operator, a postfix operator, or a binary operator. This behavior is summarized in the following rules:

If an operator has whitespace around both sides or around neither side, it is treated as a binary operator. As an example, the + operator in a+b and a + b is treated as a binary operator.

If an operator has whitespace on the left side only, it is treated as a prefix unary operator. As an example, the ++ operator in a ++b is treated as a prefix unary operator.

If an operator has whitespace on the right side only, it is treated as a postfix unary operator. As an example, the ++ operator in a++ b is treated as a postfix unary operator.

If an operator has no whitespace on the left but is followed immediately by a dot (.), it is treated as a postfix unary operator. As an[…]

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

ios – Swift error: prefix/postfix ‘=’ is reserved – Stack Overflow

swift – Is this response from the compiler valid? – Stack Overflow

swift中,对于空格,不是全部没有意义的

-》Swift用空格来区分,前缀和后缀

-》=xxx 属于一种前缀,可能意味着 对于xxx的某种使用

-》所以 上面的xxx= 也是类似的道理,被看成一种后缀,可能是xxx的某个使用(虽然暂时没有实现),但是属于语法上面的保留。

[总结]

Swift代码中,变量,定义等等的前后记得要用空格隔开,不要写成了:

yXXX,XXXy

其中y表示操作符,比如等于号=,中括号[等等

XXX表示变量

是Swift保留的前缀,后缀的写法。

改为,中间添加额外的空格:

y XXX

XXX y

比如:

上面的:

let JoinMessage_operatorName= "JoinMessage_operatorName"

改为:

let JoinMessage_operatorName = "JoinMessage_operatorName"

即可。

转载请注明:在路上 » [已解决]Swift代码出错:Postfix ‘=’ is reserved

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
85 queries in 0.185 seconds, using 22.07MB memory