【问题标题】:Issue passing a string from Swift to objective C将字符串从 Swift 传递到目标 C 的问题
【发布时间】:2015-05-28 14:07:33
【问题描述】:

这是 MyObjCClass.h 中用户名的声明:

@property (nonatomic, assign, readwrite) NSString* username;

这就是我在 MyViewController.swift 中影响它的方式:

// MyObjCObject.username = NSString(string: "homerSimpson") // not accepted by xCode : 'NSString' is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert ?
MyObjCObject.username = "homerSimpson"

这就是我在 MyObjCClass.m 中使用属性的方式:

NSLog(_username) // running error happens here

错误:

ECX_BAD_ACCESS : (code=1, address=0x0)

【问题讨论】:

  • 为什么不(非原子,强)?
  • 你甚至可以在不通过 Swift 的情况下复制它。

标签: objective-c swift nsstring interop


【解决方案1】:

您不想将assign 用作指向对象的指针。您需要将assign 更改为strongweak(或完全省略它,我相信它默认为strong),具体取决于您希望对对象进行哪种内存管理。

另外,这不是您想要使用NSLog 的方式。你需要像这样使用它:

NSLog(@"%@", _username);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多