【问题标题】:Using a data structure? [closed]使用数据结构? [关闭]
【发布时间】:2014-02-20 02:46:08
【问题描述】:

目前我有一个视图UIView *myView;。我想通过说myView.stringdata = @"whatever"; 之类的话向视图中添加数据。我怎样才能做到这一点? 对不起,我是 Xcode 新手。

【问题讨论】:

  • 你想给视图添加标签还是在视图上实际存储随机数据?
  • 我想在视图中附加一个 nsstring。不是可见的物体。 @jervine10

标签: objective-c xcode data-structures typedef


【解决方案1】:

您不能向视图添加随机数据。如果需要,您可以继承 UIView 并向类添加属性并将字符串存储在那里。或者,您可以在驱动 UIVIew 的 ViewController 中创建一个属性并将字符串分配给它。

【讨论】:

  • 这不一定是正确的,因为他可以创建一个类别,其中包含一个属性和他所希望的作品
  • @ASKASK 不正确。类别不能添加实例变量或属性。这就是苹果所说的:Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties. It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods.
  • @ASKASK 解决方法是使用关联引用。参考这个:stackoverflow.com/a/8733320/1407017
【解决方案2】:

如果类没有 ivar + 访问器或属性,则不能将数据添加到类的实例中。

您需要使用以下方法之一:

子类 类别 代表 关联对象 作品 收藏

所有这些都涉及在您的应用程序(或任何应用程序,如果它是在用户或系统级别可见的框架中的框架类)中修改整个类,或者它们涉及通过在集合成员中创建新类或关联.

【讨论】:

    猜你喜欢
    • 2018-12-22
    • 2011-08-20
    • 2020-01-25
    • 2012-11-22
    • 2011-04-17
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    相关资源
    最近更新 更多