gpt4 book ai didi

ios - 如何在 swift 中对对象实例使用选择器

转载 作者:行者123 更新时间:2023-11-28 11:52:43 25 4
gpt4 key购买 nike

正如我们所知,string 对象具有uppercasedlowercasedcapitalise 等方法。

我想在运行时调用选择器。例如:

let aSelector = Selector("capitalise")

let str = "Good Morning"
//now i want to apply aSelector on str:
let finalString = str.aSelector

我怎样才能实现它?谢谢。

最佳答案

I have two constants file for two target. In these files i want to define selector let's say capitalise and uppercase. Now in my code which is common to both targets, I have a string it can be capitalise or uppercase, without any if else. ex : str.thatSelectorInMyConstantFile

如何使用闭包:

let stringOp = { (str: String) in str.capitalized }

let stringOp = { (str: String) in str.uppercased() }

然后你会像这样使用它:

let str = "hello world"
let finalString = stringOp(str)
print(finalString)

输出:

Hello World

HELLO WORLD

关于ios - 如何在 swift 中对对象实例使用选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51643795/

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