[英]Block conversion in swift from Objective-c
How to convert following block from Objective-C
to Swift
.如何将以下块从
Objective-C
转换为Swift
。 Am using Objective-C
files in Swift
using bridge header
.我正在使用
bridge header
在Swift
使用Objective-C
文件。 But small confusion in block conversion但是块转换中的小混乱
Objective-C Block: Objective-C 块:
+ (void) while:(id)obj name:(void(^)(type*))callback;
Sample output:示例输出:
[Sub while:keeper viewControllerChanged:^(NSString* newNickname) {
NSLog(@"\nVC2- Now screen is in: %@", newNickname);
}];
How to convert this in swift
?如何在
swift
转换它?
EDIT: Swift block error is编辑: Swift 块错误是
Sub.while(obj: AnyObject!, viewControllerChanged: ((String!) -> Void)!)
When you define :当你定义:
class func while1(obj:AnyObject, callback name:((newNickname:NSString?) -> Void)) {
}
And when call function :当调用函数时:
self.while1(self) { (newNickname) -> Void in
print("\nVC2- Now screen is in:" + "\(newNickname)")
}
EDIT :编辑 :
Okay, Then you just want to call it from swift..right..?好的,那么您只想从 swift 调用它..对..? Then use this statement :
然后使用这个语句:
ClassName.while1(obj as AnyObject) { (nickName:String!) -> Void in
print(nickName)
}
But first make sure that in your definition statement "type" indicates for what DataType, so please define there actual DataType但首先要确保在你的定义语句中“类型”表示什么数据类型,所以请在那里定义实际的数据类型
+ (void)while:(id)obj name:(void(^)(type*))callback;
to --> For example :到 --> 例如:
+ (void)while1:(id)obj name:(void(^)(NSString *))callback;
And one more thing to note that while
in built in keyword, please do not use it if possible.还有一两件事要注意,
while
在建的关键字,请不要如果可能的话使用它。
You can call like this:你可以这样调用:
YourClassName.while2(Yourparameter , name: {(nickName : String) -> Void in
})
I hope this help.我希望这会有所帮助。
I'm not sure if you are asking how to write blocks in swift or if I'm not completely getting your question.我不确定您是在问如何快速编写块,还是我没有完全理解您的问题。 But if it's the first then...
但是如果是第一次的话...
(void(^)(type*))callback
becomes变成
callback: (param:type*)->(returnType*)
ie IE
func doSomething(callback:((number:Int)->()))
is called like被称为像
doSomething(callback:{number in
print("\(number)")
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.