【问题标题】:How to create array object and update it's value [closed]如何创建数组对象并更新它的值
【发布时间】:2021-06-25 23:26:09
【问题描述】:

我来自 Javascript 背景,我的界面/实现对我来说是新的。

假设我有一个参与者,其接口和实现如下所示

Participant.h 文件

#import <Foundation/Foundation.h>

@interface Participant : NSObject
    @property(nonatomic, assign, readonly) NSString *name;
    @property(nonatomic, assign, readonly) NSString *id;
    @property(nonatomic, assign, readonly) NSString *gender;
+ (instancetype _Nonnull)sharedInstance;

@end

Participant.m 文件

@implementation Participant


- (instancetype)init {
    if (self = [super init]) {
        [self setAllValueNil];
        [self initMessageEmit];
    }
    return self;
}



-(void)setAllValueNil {
    _name = nil;
    _id = nil;
    _gender = nil;
}


- (void)initMessageEmit {
    [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(receiveNotification:)
            name:@"participantReciever"
            object:nil];
}

+ (instancetype)sharedInstance {
      static Participant *sharedInstance = nil;
      static dispatch_once_t OnceToken;
      dispatch_once(OnceToken, ^{
          sharedInstance = [[self alloc] init];
      });
      return sharedInstance;
}

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void) receiveNotification:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"participantReciever"]) {
        if (notification.userInfo[@"value"] != nil) {
            if ([notification.userInfo[@"type"] isEqualToString:@"setName"]) {
            _name = notification.userInfo[@"value"]
          } 
        }
     }

此代码适用于单个参与者,但如果我有多个参与者,我将如何编写上述代码?有人可以分享一个例子吗?其中 id 是唯一键

【问题讨论】:

  • 不清楚问题是什么。如果您有多个参与者,您将丢弃您的 sharedInstance 方法,这会使其成为单例。如果你需要我的建议,无论如何你都应该把它扔掉。即使在 JavaScript 中也有类和实例的概念(有点)。 ——我以前建议过这个;为什么不学习 Objective-C 而不是折腾?
  • 无需显式设置属性为nil;这是为你完成的。正如@matt 所说,这个对象没有理由成为单例,特别是因为你已经说过你想要多个实例。使用通知集你的属性也是不必要的复杂;您通常会引用对象实例并简单地设置属性。您可以使用 uuid 作为唯一键,尽管这对于内存中的对象来说是不寻常的。您可以将它传递给初始化程序,也可以让初始化程序随机分配一个。
  • 我注意到您已将属性设置为只读。这消除了简单的属性设置器。这真的是您的意图吗,因为您在问题标题中询问“如何更新其价值”?你能edit你的问题更清楚地解释你想要做什么吗?
  • 嘿@matt!感谢您的评论。当然,这是最好的方法,我会这样做。上面的代码 sn-p 适用于单个参与者,而不是参与者

标签: ios objective-c xcode


【解决方案1】:

我首先要说的是上面 @matt@Paulw11 建议的方法。如果需要这种类型的对象数组,则不需要将其设为单例。

同时在界面中将属性设置为只读并且只允许通过 NotificationCenter 进行修改不是最好的主意。假设您要在 10 个实例上修改一个值,您将需要触发 10 个通知并处理它们。对于这种类型的实例数组,这会变得不必要地复杂。


继续,您正在添加这样的通知观察者 -

[[NSNotificationCenter defaultCenter] 
    addObserver: self
    selector: @selector(receiveNotification:)
    name: @"participantReciever"
    object: nil
];

当您指定object: nil 时,您的selector 将在没有任何实例过滤的情况下被调用。假设您在内存中有 10 个此类实例,则会发布一个名为 "participantReciever" 的通知(所有 10 个实例都会收到此通知)。

你可以做的是 -

[[NSNotificationCenter defaultCenter] 
    addObserver: self
    selector: @selector(receiveNotification:)
    name: @"participantReciever"
    object: self // note this
];

现在您指定object: self,您将只收到影响此特定实例的通知,假设您在发布通知时指定object: &lt;object_that_you_want_to_modify&gt;

[[NSNotificationCenter defaultCenter] 
    postNotificationName: @"participantReciever",
    object: Participant.sharedInstance, // note this
    userInfo: @{@"name": @"Test"}
];

【讨论】:

  • 谢谢塔伦,这非常有帮助。我现在想坚持消息发送模式,我也希望任何人都订阅这些通知。你能帮我解决一下有对象数组的接口吗? (参与者)
  • 当您对多个实例使用上述设置时,只需记住通过 postNotificationName: 调用在 object: &lt;instance_you_wish_to_modify&gt; 中传递正确的实例,并且不需要任何额外的 id 检查。
猜你喜欢
  • 2018-12-28
  • 2020-11-15
  • 1970-01-01
  • 2020-01-24
  • 2021-09-24
  • 1970-01-01
  • 2021-08-12
  • 1970-01-01
  • 2021-05-21
相关资源
最近更新 更多