【问题标题】:Objective-C: multi-parameter method syntax into Swift Package's functionsObjective-C:Swift Package 函数中的多参数方法语法
【发布时间】:2021-07-31 21:45:17
【问题描述】:

目标:掌握 ObjC Swift API 语法
问题: 使用多参数函数 API。

我已经很多年没有做 ObjC 了,现在我需要将 ObjC 与 Swift 包接口。
这是 Swift-Package 代码:

import Foundation

public struct RicStruct {
    public private(set) var text = "Hello, World!"

    public init() {}
    
    public func sayHello() -> String {
        "Hello Ric!"
    }
}

public class RicClass: NSObject {
    @objc public var msg = "Mother has a feeling, I might be too appealing."
    @objc public let text = "Hello Everybody!"
    
    override public init() {}
    
    @objc public func sayHello() {
        print(text)
    }
    
    @objc public func doSomething(msg: String) {
        print("Inside doSomething: \(msg)")
    }
    
    @objc public func doSomething(msg: String, answer: String) {
        print("Inside doSomething with msg: \(msg) and answer: \(answer)")
    }
    
    @objc public func sayHelloTo(whom: String) -> String {
        "Hello \(whom)!"
    }
}

我在使用多参数语法时遇到了问题。
Objective-C 喜欢将 'with' 或 'to' 等附加到参数名称。我通过反复试验了解到这一点。

但我在使用多参数语法时遇到了问题:

问题:多参数Objective-C方法调用的正确语法是什么?

【问题讨论】:

  • 你试过了吗:[richClass doSomethingWithMsg:@"hello",andAnswer:@"world"];或者只是:[richClass doSomethingWithMsg:@"hello", answer:@"world"];

标签: ios swift objective-c swift-package


【解决方案1】:

简单的解决方案,添加适当的标签:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2011-11-30
    • 2015-01-12
    相关资源
    最近更新 更多