问题
I am using a library MDWamp written in objective C and it has a property of the following type
@property (nonatomic, strong) void (^deferredWampCRASigningBlock)( NSString *challange, void(^finishBLock)(NSString *signature) );
This is the signature in swift
public var deferredWampCRASigningBlock: ((String!, ((String!) -> Void)!) -> Void)!
and when I try to instantiate it in swift in the following manner
self.wamp?.config.deferredWampCRASigningBlock?(str : String , { (str2 : String) -> Void in
})
but I get this error
Cannot convert value of type 'String.Type' to expected argument type 'String!'
Any suggestions would be appreciated.
回答1:
Lets walk through what deferredWampCRASigningBlock
is:
((String!, ((String!) -> Void)!) -> Void)!
This is a void function that takes two things:
- a String!
- a void function that takes a String!
So when you call it, you must pass it those things. A string and a function.
let challenge = "challenge"
let finishBLock: String! -> Void = { signature in }
self.wamp?.config.deferredWampCRASigningBlock?(challenge, finishBlock)
From some of your comments, you seem to not know what challenge
should be at this point. That suggests you should not be calling this function. This function is intended to be called by the part of the program that does know challenge
is.
The confusion may be related to "when I try to instantiate it." The code you've given doesn't instantiate anything. It is trying to call the function. Perhaps what you really meant was to create the function and assign it:
self.wamp?.config.deferredWampCRASigningBlock =
{ (challenge: String!, finishBlock: ((String!) -> Void)!) -> Void in
// ...
}
回答2:
It means you're passing the data type. Please pass the value.
回答3:
Try this
self.wamp?.config.deferredWampCRASigningBlock = {( challenge: String!, finishBlock) -> Void in
//calculate signature using any algorithm and return in finishBlock see below example
let sign = challenge.hmacSHA256DataWithKey(“secretKey”)
finishBlock(sign)
}
回答4:
You'r passing String.Type
not string value.
Instead of:
self.wamp?.config.deferredWampCRASigningBlock?(str : String , { (str2 : String) -> Void in
})
It should be:
self.wamp?.config.deferredWampCRASigningBlock?(str : "some string" , { (str2 : String) -> Void in
})
回答5:
cell.lblname?.text = String(describing: tasks[indexPath.row])
cell.lbladdress?.text = String(describing: tasks[indexPath.row])
cell.lblphone?.text = String(describing: tasks[indexPath.row])
来源:https://stackoverflow.com/questions/33869604/cannot-convert-value-of-type-string-type-to-expected-argument-type-string