gpt4 book ai didi

swift - 我应该如何决定我的函数是应该返回可选的还是隐式解包的可选的?

转载 作者:可可西里 更新时间:2023-11-01 00:38:22 25 4
gpt4 key购买 nike

如果我需要返回一些可以为 nil 的东西,我应该如何决定我的 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
}

最佳答案

现在为 swift 制定严格的最佳实践可能还为时过早,但这是我的观点。一般来说,隐式展开的可选值应该用于一些情况,例如 socket 、在初始化之前为 nil 的属性和 Objective-C 函数的返回值,否则应该使用常规可选值。

如果您正在创建一个将返回值或 nil 的函数,请帮其他与您的代码交互的程序员一个忙,并使返回值成为可选值。

关于swift - 我应该如何决定我的函数是应该返回可选的还是隐式解包的可选的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24481931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com