How do I bridge objc typedef NSString to Swift as String?
up vote
-1
down vote
favorite
Say I have some objc code:
typedef NSString * ObjcNSString;
@interface ObjC : NSObject
+ (void)doThingsWithString:(nullable ObjcNSString)mid;
@end
The corresponding generated Swift interface for the objc code above is:
public typealias ObjcNSString = NSString
open class ObjC : NSObject {
open class func doThings(with mid: String?)
}
This is causing problem if I use the code in Swift:
let str: ObjcNSString? = nil
ObjC.doThings(with: str) // Cannot convert value of type 'ObjcNSString?' (aka 'Optional<NSString>') to expected argument type 'String?'
But I really want to use the ObjcNSString
type in Swift, is there any way to make it work?
swift
add a comment |
up vote
-1
down vote
favorite
Say I have some objc code:
typedef NSString * ObjcNSString;
@interface ObjC : NSObject
+ (void)doThingsWithString:(nullable ObjcNSString)mid;
@end
The corresponding generated Swift interface for the objc code above is:
public typealias ObjcNSString = NSString
open class ObjC : NSObject {
open class func doThings(with mid: String?)
}
This is causing problem if I use the code in Swift:
let str: ObjcNSString? = nil
ObjC.doThings(with: str) // Cannot convert value of type 'ObjcNSString?' (aka 'Optional<NSString>') to expected argument type 'String?'
But I really want to use the ObjcNSString
type in Swift, is there any way to make it work?
swift
1
Why do you have thetypedef
andtypealias
? Why do you want to useNSString
in Swift?
– rmaddy
Nov 9 at 3:36
Also why are implementing same class(ObjC) in both Swift and ObjectiveC?. Also you are trying to call Swift class function only by "ObjC.doThings(with: str)". So It should expect "String?" parameter only.
– Natarajan
Nov 9 at 3:39
I'm not implementing the same class in both Swift and Objective-C, the Swift code forObjC
class is the generated Swift interface for the objc code.
– axl411
Nov 12 at 2:43
You really want to get rid of theObjcNSString
typedef. That's just creating a mess of everything here. It's not the underlying problem; it doesn't break anything itself; it's just causing you confusion that's leading you to write incorrect code for no reason (as matt explains below). UseNSString *
in ObjC andString
in Swift, and everything will just work. (There is definitely ways to makeObjcNSString
work in Swift; it does work in Swift; but it's going to constantly bite you for no reason. You'd better have a really compelling use care here that you should explain.)
– Rob Napier
Nov 12 at 4:41
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Say I have some objc code:
typedef NSString * ObjcNSString;
@interface ObjC : NSObject
+ (void)doThingsWithString:(nullable ObjcNSString)mid;
@end
The corresponding generated Swift interface for the objc code above is:
public typealias ObjcNSString = NSString
open class ObjC : NSObject {
open class func doThings(with mid: String?)
}
This is causing problem if I use the code in Swift:
let str: ObjcNSString? = nil
ObjC.doThings(with: str) // Cannot convert value of type 'ObjcNSString?' (aka 'Optional<NSString>') to expected argument type 'String?'
But I really want to use the ObjcNSString
type in Swift, is there any way to make it work?
swift
Say I have some objc code:
typedef NSString * ObjcNSString;
@interface ObjC : NSObject
+ (void)doThingsWithString:(nullable ObjcNSString)mid;
@end
The corresponding generated Swift interface for the objc code above is:
public typealias ObjcNSString = NSString
open class ObjC : NSObject {
open class func doThings(with mid: String?)
}
This is causing problem if I use the code in Swift:
let str: ObjcNSString? = nil
ObjC.doThings(with: str) // Cannot convert value of type 'ObjcNSString?' (aka 'Optional<NSString>') to expected argument type 'String?'
But I really want to use the ObjcNSString
type in Swift, is there any way to make it work?
swift
swift
edited Nov 12 at 2:40
asked Nov 9 at 3:32
axl411
462418
462418
1
Why do you have thetypedef
andtypealias
? Why do you want to useNSString
in Swift?
– rmaddy
Nov 9 at 3:36
Also why are implementing same class(ObjC) in both Swift and ObjectiveC?. Also you are trying to call Swift class function only by "ObjC.doThings(with: str)". So It should expect "String?" parameter only.
– Natarajan
Nov 9 at 3:39
I'm not implementing the same class in both Swift and Objective-C, the Swift code forObjC
class is the generated Swift interface for the objc code.
– axl411
Nov 12 at 2:43
You really want to get rid of theObjcNSString
typedef. That's just creating a mess of everything here. It's not the underlying problem; it doesn't break anything itself; it's just causing you confusion that's leading you to write incorrect code for no reason (as matt explains below). UseNSString *
in ObjC andString
in Swift, and everything will just work. (There is definitely ways to makeObjcNSString
work in Swift; it does work in Swift; but it's going to constantly bite you for no reason. You'd better have a really compelling use care here that you should explain.)
– Rob Napier
Nov 12 at 4:41
add a comment |
1
Why do you have thetypedef
andtypealias
? Why do you want to useNSString
in Swift?
– rmaddy
Nov 9 at 3:36
Also why are implementing same class(ObjC) in both Swift and ObjectiveC?. Also you are trying to call Swift class function only by "ObjC.doThings(with: str)". So It should expect "String?" parameter only.
– Natarajan
Nov 9 at 3:39
I'm not implementing the same class in both Swift and Objective-C, the Swift code forObjC
class is the generated Swift interface for the objc code.
– axl411
Nov 12 at 2:43
You really want to get rid of theObjcNSString
typedef. That's just creating a mess of everything here. It's not the underlying problem; it doesn't break anything itself; it's just causing you confusion that's leading you to write incorrect code for no reason (as matt explains below). UseNSString *
in ObjC andString
in Swift, and everything will just work. (There is definitely ways to makeObjcNSString
work in Swift; it does work in Swift; but it's going to constantly bite you for no reason. You'd better have a really compelling use care here that you should explain.)
– Rob Napier
Nov 12 at 4:41
1
1
Why do you have the
typedef
and typealias
? Why do you want to use NSString
in Swift?– rmaddy
Nov 9 at 3:36
Why do you have the
typedef
and typealias
? Why do you want to use NSString
in Swift?– rmaddy
Nov 9 at 3:36
Also why are implementing same class(ObjC) in both Swift and ObjectiveC?. Also you are trying to call Swift class function only by "ObjC.doThings(with: str)". So It should expect "String?" parameter only.
– Natarajan
Nov 9 at 3:39
Also why are implementing same class(ObjC) in both Swift and ObjectiveC?. Also you are trying to call Swift class function only by "ObjC.doThings(with: str)". So It should expect "String?" parameter only.
– Natarajan
Nov 9 at 3:39
I'm not implementing the same class in both Swift and Objective-C, the Swift code for
ObjC
class is the generated Swift interface for the objc code.– axl411
Nov 12 at 2:43
I'm not implementing the same class in both Swift and Objective-C, the Swift code for
ObjC
class is the generated Swift interface for the objc code.– axl411
Nov 12 at 2:43
You really want to get rid of the
ObjcNSString
typedef. That's just creating a mess of everything here. It's not the underlying problem; it doesn't break anything itself; it's just causing you confusion that's leading you to write incorrect code for no reason (as matt explains below). Use NSString *
in ObjC and String
in Swift, and everything will just work. (There is definitely ways to make ObjcNSString
work in Swift; it does work in Swift; but it's going to constantly bite you for no reason. You'd better have a really compelling use care here that you should explain.)– Rob Napier
Nov 12 at 4:41
You really want to get rid of the
ObjcNSString
typedef. That's just creating a mess of everything here. It's not the underlying problem; it doesn't break anything itself; it's just causing you confusion that's leading you to write incorrect code for no reason (as matt explains below). Use NSString *
in ObjC and String
in Swift, and everything will just work. (There is definitely ways to make ObjcNSString
work in Swift; it does work in Swift; but it's going to constantly bite you for no reason. You'd better have a really compelling use care here that you should explain.)– Rob Napier
Nov 12 at 4:41
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Just about everything in this question is a red herring. The only important thing about the error you're getting is the attempt to use an Optional<NSString>
where an Optional<String>
is expected. You can't do that. The matter has nothing to do with Objective-C; you can see for yourself in pure Swift:
let s : NSString? = nil
let s2 : String? = s // error
You seem to assume these two types, Optional<NSString>
and Optional<String>
, are somehow magically interchangeable. They are not.
But they are castable one to the other:
let s : NSString? = nil
let s2 : String? = s as String?
Thus you could solve your compile error by writing:
let str: ObjcNSString? = nil
ObjC.doThings(with: str as String?)
But I really want to use the ObjcNSString type in Swift
It's not quite clear what you can mean by that. It isn't a Swift type. It's an Objective-C type, and as an Objective-C type it is a synonym for NSString. Any place where NSString is expected by Objective-C, Swift will expect a String when using that API. Trying to substitute another name for NSString doesn't affect that fact.
Nor is it clear what your ultimate goals can be. You can pass a string from Swift where an ObjcNSString is expected by Objective-C, so what's the problem?
add a comment |
up vote
1
down vote
It looks like the code that was generated is not quite what you want.
The error you are getting is saying that your function is expecting a String
type, but you are trying to use a ObjcNSString
type.
To fix this, just change your function declaration to:
open class func doThings(with mid: ObjcNSString?)
But keep in mind, you can just use NSString in Swift. You don't need the typealias. You can just as well write your function like:
open class func doThings(with mid: NSString?)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Just about everything in this question is a red herring. The only important thing about the error you're getting is the attempt to use an Optional<NSString>
where an Optional<String>
is expected. You can't do that. The matter has nothing to do with Objective-C; you can see for yourself in pure Swift:
let s : NSString? = nil
let s2 : String? = s // error
You seem to assume these two types, Optional<NSString>
and Optional<String>
, are somehow magically interchangeable. They are not.
But they are castable one to the other:
let s : NSString? = nil
let s2 : String? = s as String?
Thus you could solve your compile error by writing:
let str: ObjcNSString? = nil
ObjC.doThings(with: str as String?)
But I really want to use the ObjcNSString type in Swift
It's not quite clear what you can mean by that. It isn't a Swift type. It's an Objective-C type, and as an Objective-C type it is a synonym for NSString. Any place where NSString is expected by Objective-C, Swift will expect a String when using that API. Trying to substitute another name for NSString doesn't affect that fact.
Nor is it clear what your ultimate goals can be. You can pass a string from Swift where an ObjcNSString is expected by Objective-C, so what's the problem?
add a comment |
up vote
3
down vote
accepted
Just about everything in this question is a red herring. The only important thing about the error you're getting is the attempt to use an Optional<NSString>
where an Optional<String>
is expected. You can't do that. The matter has nothing to do with Objective-C; you can see for yourself in pure Swift:
let s : NSString? = nil
let s2 : String? = s // error
You seem to assume these two types, Optional<NSString>
and Optional<String>
, are somehow magically interchangeable. They are not.
But they are castable one to the other:
let s : NSString? = nil
let s2 : String? = s as String?
Thus you could solve your compile error by writing:
let str: ObjcNSString? = nil
ObjC.doThings(with: str as String?)
But I really want to use the ObjcNSString type in Swift
It's not quite clear what you can mean by that. It isn't a Swift type. It's an Objective-C type, and as an Objective-C type it is a synonym for NSString. Any place where NSString is expected by Objective-C, Swift will expect a String when using that API. Trying to substitute another name for NSString doesn't affect that fact.
Nor is it clear what your ultimate goals can be. You can pass a string from Swift where an ObjcNSString is expected by Objective-C, so what's the problem?
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Just about everything in this question is a red herring. The only important thing about the error you're getting is the attempt to use an Optional<NSString>
where an Optional<String>
is expected. You can't do that. The matter has nothing to do with Objective-C; you can see for yourself in pure Swift:
let s : NSString? = nil
let s2 : String? = s // error
You seem to assume these two types, Optional<NSString>
and Optional<String>
, are somehow magically interchangeable. They are not.
But they are castable one to the other:
let s : NSString? = nil
let s2 : String? = s as String?
Thus you could solve your compile error by writing:
let str: ObjcNSString? = nil
ObjC.doThings(with: str as String?)
But I really want to use the ObjcNSString type in Swift
It's not quite clear what you can mean by that. It isn't a Swift type. It's an Objective-C type, and as an Objective-C type it is a synonym for NSString. Any place where NSString is expected by Objective-C, Swift will expect a String when using that API. Trying to substitute another name for NSString doesn't affect that fact.
Nor is it clear what your ultimate goals can be. You can pass a string from Swift where an ObjcNSString is expected by Objective-C, so what's the problem?
Just about everything in this question is a red herring. The only important thing about the error you're getting is the attempt to use an Optional<NSString>
where an Optional<String>
is expected. You can't do that. The matter has nothing to do with Objective-C; you can see for yourself in pure Swift:
let s : NSString? = nil
let s2 : String? = s // error
You seem to assume these two types, Optional<NSString>
and Optional<String>
, are somehow magically interchangeable. They are not.
But they are castable one to the other:
let s : NSString? = nil
let s2 : String? = s as String?
Thus you could solve your compile error by writing:
let str: ObjcNSString? = nil
ObjC.doThings(with: str as String?)
But I really want to use the ObjcNSString type in Swift
It's not quite clear what you can mean by that. It isn't a Swift type. It's an Objective-C type, and as an Objective-C type it is a synonym for NSString. Any place where NSString is expected by Objective-C, Swift will expect a String when using that API. Trying to substitute another name for NSString doesn't affect that fact.
Nor is it clear what your ultimate goals can be. You can pass a string from Swift where an ObjcNSString is expected by Objective-C, so what's the problem?
edited Nov 12 at 3:00
answered Nov 12 at 2:54
matt
317k44512714
317k44512714
add a comment |
add a comment |
up vote
1
down vote
It looks like the code that was generated is not quite what you want.
The error you are getting is saying that your function is expecting a String
type, but you are trying to use a ObjcNSString
type.
To fix this, just change your function declaration to:
open class func doThings(with mid: ObjcNSString?)
But keep in mind, you can just use NSString in Swift. You don't need the typealias. You can just as well write your function like:
open class func doThings(with mid: NSString?)
add a comment |
up vote
1
down vote
It looks like the code that was generated is not quite what you want.
The error you are getting is saying that your function is expecting a String
type, but you are trying to use a ObjcNSString
type.
To fix this, just change your function declaration to:
open class func doThings(with mid: ObjcNSString?)
But keep in mind, you can just use NSString in Swift. You don't need the typealias. You can just as well write your function like:
open class func doThings(with mid: NSString?)
add a comment |
up vote
1
down vote
up vote
1
down vote
It looks like the code that was generated is not quite what you want.
The error you are getting is saying that your function is expecting a String
type, but you are trying to use a ObjcNSString
type.
To fix this, just change your function declaration to:
open class func doThings(with mid: ObjcNSString?)
But keep in mind, you can just use NSString in Swift. You don't need the typealias. You can just as well write your function like:
open class func doThings(with mid: NSString?)
It looks like the code that was generated is not quite what you want.
The error you are getting is saying that your function is expecting a String
type, but you are trying to use a ObjcNSString
type.
To fix this, just change your function declaration to:
open class func doThings(with mid: ObjcNSString?)
But keep in mind, you can just use NSString in Swift. You don't need the typealias. You can just as well write your function like:
open class func doThings(with mid: NSString?)
answered Nov 12 at 2:47
Alan Scarpa
1,71831340
1,71831340
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219460%2fhow-do-i-bridge-objc-typedef-nsstring-to-swift-as-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Why do you have the
typedef
andtypealias
? Why do you want to useNSString
in Swift?– rmaddy
Nov 9 at 3:36
Also why are implementing same class(ObjC) in both Swift and ObjectiveC?. Also you are trying to call Swift class function only by "ObjC.doThings(with: str)". So It should expect "String?" parameter only.
– Natarajan
Nov 9 at 3:39
I'm not implementing the same class in both Swift and Objective-C, the Swift code for
ObjC
class is the generated Swift interface for the objc code.– axl411
Nov 12 at 2:43
You really want to get rid of the
ObjcNSString
typedef. That's just creating a mess of everything here. It's not the underlying problem; it doesn't break anything itself; it's just causing you confusion that's leading you to write incorrect code for no reason (as matt explains below). UseNSString *
in ObjC andString
in Swift, and everything will just work. (There is definitely ways to makeObjcNSString
work in Swift; it does work in Swift; but it's going to constantly bite you for no reason. You'd better have a really compelling use care here that you should explain.)– Rob Napier
Nov 12 at 4:41