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?










share|improve this question




















  • 1




    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










  • 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

















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?










share|improve this question




















  • 1




    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










  • 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















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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 2:40

























asked Nov 9 at 3:32









axl411

462418




462418








  • 1




    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










  • 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
















  • 1




    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










  • 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










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














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?






share|improve this answer






























    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?)






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      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

























      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?






      share|improve this answer



























        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?






        share|improve this answer

























          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?






          share|improve this answer














          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?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 3:00

























          answered Nov 12 at 2:54









          matt

          317k44512714




          317k44512714
























              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?)






              share|improve this answer

























                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?)






                share|improve this answer























                  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?)






                  share|improve this answer












                  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?)







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 2:47









                  Alan Scarpa

                  1,71831340




                  1,71831340






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Schultheiß

                      Verwaltungsgliederung Dänemarks

                      Liste der Kulturdenkmale in Wilsdruff