gpt4 book ai didi

ios - (ObjC -> Swift) 转换函数 "init"& "textureNamed"

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:30 25 4
gpt4 key购买 nike

当我尝试在 Swift 中转换 2 个函数时出现一些错误!

ObjC

@property (nonatomic, strong) NSMutableDictionary *textureCache;    

- (instancetype)init {
if (self = [super init])
{
self.textureCache = [NSMutableDictionary dictionary];
}
return self;
}

- (SKTexture *)textureNamed:(NSString *)textureName {
return _textureCache[textureName];
}

swift

var textureCache: NSMutableDictionary?    

public func init() -> Self {
return textureCache = NSMutableDictionary()
}

public func textureNamed(textureName: String!) -> SKTexture! {
return textureCache(textureName)
}

在“初始化”函数中:

  1. 函数声明中的预期标识符 -> (public func init -> self )
  2. 无法将“NSMutableDictionary”类型的值分配给“NSMutableDictionary”类型的值? -> (返回 textureCache = NSMutableDictionary())

对于“textureNamed”函数:

  1. 无法使用“(String!)”类型的参数列表调用“textureCache”->(返回纹理缓存(纹理名称))

如果有人能帮助我,那就太棒了。

最佳答案

我会尝试这样的事情:

class Test: NSObject {

var textureCache = [String:SKTexture]()

override init() {
super.init()
}

func textureNamed(name: String) -> SKTexture? {
return self.textureCache[name]
}

}

关于ios - (ObjC -> Swift) 转换函数 "init"& "textureNamed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29603444/

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