初始化 NSMutableArray 时使用简短的语法是否正确? [英] Is it correct to use short syntax when initializing NSMutableArray?

查看:47
本文介绍了初始化 NSMutableArray 时使用简短的语法是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常当我们想要初始化 NSMutableArray 时,我们使用:

Usually when we want to initialize NSMutableArray we use:

NSMutableArray *mArr = [[NSMutableArray alloc] initWithObjects: @"one", @"two", @"three", nil];

但是,使用以下语法是否正确:

But, is it correct to use syntax like:

NSMutableArray *mArr = @[@"one", @"two", @"three"].mutableCopy;

我知道,它的工作时间会延长几纳秒.但我认为第二种方式更具可读性,我准备牺牲这些纳秒.

I understand, that it will work a couple of nanoseconds longer. But I think the second way to be way more readable and I'm ready to sacrifice those nanoseconds.

使用这种结构可以吗?ARC 会清除我用来获取可变副本的未使用 NSArray 吗?不会漏水吗?

Is it ok, to use this kind of construction? Does ARC clean that unused NSArray, that I'm using to get a mutable copy? Isn't it going to be a leak?

推荐答案

但我认为第二种方式更具可读性

But I think the second way to be way more readable

就我个人而言,我觉得它更令人困惑,甚至更令人困惑,因为您将 mutableCopy 当作一个属性来使用.正确,正确,但完全误导恕我直言.

Personally, I find it more confusing, and even more confusing since you are using mutableCopy as if it was a property. Correct, correct, but totally misleading IMHO.

为什么不直接利用继承?

NSMutableArray *ma = [NSMutableArray arrayWithObjects:@"foo", @"bar", nil];

有时默认情况下可变的集合会更有用.* 叹气 *

这篇关于初始化 NSMutableArray 时使用简短的语法是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆