如果我需要返回可以为零的内容,我应该如何判断我的func是否应该返回可选或隐式解包的可选项?我已经看过任何一个版本的Swift方法,我很好奇是什么驱动决定从函数/方法返回一个可选的与一个隐式展开的可选项,因为它看起来像你需要检查非nil才能使用它
例如,SpriteKit的SKNode使用隐式展开(下面显示的部分版本)
class SKNode : UIResponder, NSCopying, NSCoding {
...
/**
The parent of the node.
If this is nil the node has not been added to another group and is thus the root node of its own graph.
*/
var parent: SKNode! { get }
/**
The children of this node.
*/
var children: AnyObject[]! { get }
/**
The client assignable name.
In general, this should be unique among peers in the scene graph.
*/
var name: String!
/**
The scene that the node is currently in.
*/
var scene: SKScene! { get }
/**
Physics body attached to the node, with synchronized scale, rotation, and position
*/
var physicsBody: SKPhysicsBody!
/**
An optional dictionary that can be used to hold user data pretaining to the node. Defaults to nil.
*/
var userData: NSMutableDictionary!
/**
Kinematic constraints, used in IK solving
*/
var reachConstraints: SKReachConstraints!
/**
Optional array of SKConstraints
Constraints are evaluated each frame after actions and physics.
The node's transform will be changed to staisfy the constarint.
*/
var constraints: AnyObject[]!
/**
Sets both the x & y scale
@param scale the uniform scale to set.
*/
func setScale(scale: CGFloat)
/**
Adds a node as a child node of this node
The added node must not have a parent.
@param node the child node to add.
*/
func addChild(node: SKNode!)
func insertChild(node: SKNode!, atIndex index: Int)
func removeChildrenInArray(nodes: AnyObject[]!)
func removeAllChildren()
func removeFromParent()
func childNodeWithName(name: String!) -> SKNode!
func enumerateChildNodesWithName(name: String!, usingBlock block: ((SKNode!, CMutablePointer<ObjCBool>) -> Void)!)
/* Returns true if the specified parent is in this node's chain of parents */
func inParentHierarchy(parent: SKNode!) -> Bool
func runAction(action: SKAction!)
func runAction(action: SKAction!, completion block: (() -> Void)!)
func runAction(action: SKAction!, withKey key: String!)
func hasActions() -> Bool
func actionForKey(key: String!) -> SKAction!
func removeActionForKey(key: String!)
func removeAllActions()
func containsPoint(p: CGPoint) -> Bool
func nodeAtPoint(p: CGPoint) -> SKNode!
func nodesAtPoint(p: CGPoint) -> AnyObject[]!
func convertPoint(point: CGPoint, fromNode node: SKNode!) -> CGPoint
func convertPoint(point: CGPoint, toNode node: SKNode!) -> CGPoint
/* Returns true if the bounds of this node intersects with the transformed bounds of the other node, otherwise false */
func intersectsNode(node: SKNode!) -> Bool
}
答案 0 :(得分:3)
对于迅速采用严格的最佳做法可能为时尚早,但这是我的看法。一般情况下,隐式解包的选项应该用于少数情况,例如outlet,在初始化之前为零的属性以及从Objective-C函数返回值,否则应该使用常规可选项。
如果要创建一个返回值或nil的函数,请让其他程序员与您的代码进行交互,并使返回值成为可选项。
答案 1 :(得分:2)
我觉得斯威夫特此刻对于这种建议有点太年轻了,所以请带上一粒盐。并且在未来谁知道,也许Swift会有一个新功能改变每个人编写Swift代码的方式。
话虽如此,这是我对这个主题的看法:
在原生Swift代码中,您应该尽可能远离隐式展开的选项。如果您的函数可能因任何原因返回nil
,则返回类型应为Optional,告诉接收者他们可以期望返回nil
。
从本机Swift函数返回一个隐式解包的可选项目前并没有提供太多信息。对我来说它说“这个返回值永远不应该是nil
”。但在那种情况下,为什么它需要一个可选的呢?
修改强> 关于您发布的代码,代码返回隐式解包的选项的唯一原因是因为它是从ObjC转换的。
答案 2 :(得分:0)
在我看来,对于旧框架,苹果使用隐式展开选项的原因是提供与Obj-C几乎相同的用法和行为。