Swift 4 将多个对象添加到数组的快捷语法,并设置了一些属性?
Swift 4 shortcut syntax to add multiple objects, with some properities set, to an array?
是否有一些 shorthand 语法允许我添加一个对象并将其属性设置为一个数组。这就是我目前正在做的,但是我想将这些对象全部附加到一个附加语句中。
我尝试过各种语法,使用 self()
,但运气不佳。
var selectFields = [iCloudSchemaFieldValue]()
let tid = iCloudSchemaFieldValue()
tid.setField(LoggerStartup.eTblTypes.tid)
selectFields.append(tid)
let name = iCloudSchemaFieldValue()
name.setField(LoggerStartup.eTblTypes.name)
selectFields.append(name)
这是我的枚举
public enum eTblTypes
{
public static let tid = 2010
public static let name = 2020
//
}
这是我的 Objective C 框架 class..
@interface iCloudSchemaFieldValue : NSObject
@property (nonatomic) NSInteger enumVal;
@property (nonatomic) NSInteger tableEnumVal;
@property (nonatomic) id value;
@property (nonatomic) BOOL sysGenDefault;
@property (nonatomic) eOperator whereOperator;
@property (nonatomic) eOperator whereCondition;
@property (nonatomic) NSInteger padAmount;
-(id)setField: (NSInteger)ev;
-(id)setFieldVal: (NSInteger)ev
value: (id)value;
-(id)setFieldWithPadding: (NSInteger)ev
padding: (NSInteger)padding;
-(id)setFieldSysGenVal: (NSInteger)ev;
-(id)setWhereVal: (NSInteger)ev
table: (NSInteger)tableEv
operator: (eOperator)op
value: (id)value
condition: (eOperator)condition;
@end
我真的不想向这个 class 添加 init 方法,我有几个辅助方法,我想在其中使用相同的简写语法方法。
我不知道这样会不会好很多,但你可以这样做:
let selectFields: [iCloudSchemaFieldValue] = [
LoggerStartup.eTblTypes.tid,
LoggerStartup.eTblTypes.name,
... // add other fields you want to set here
].map {
let obj = iCloudSchemaFieldValue()
obj.setField([=10=])
return obj
}
我假设 LoggerStartup.eTblTypes.tid
和 LoggerStartup.eTblTypes.name
是同一类型 Int
,并且 iCloudSchemaFieldValue
是一个类型。
现在每次要设置一个新字段时,只需多添加一行,而不是 3 行。
如果您可以编辑 iCloudSchemaFieldValue
以添加采用 Int
的初始化程序,那将是最好的。然后你可以这样做:
let selectFields = [
LoggerStartup.eTblTypes.tid,
LoggerStartup.eTblTypes.name,
... // add other fields you want to set here
].map(iCloudSchemaFieldValue.init)
I'd like to append these objects all in one append statement.
你可以这样做:
selectFields = [tid, name]
但由于您的数据源 selectFields
已经初始化,这样就可以了:
selectFields.insert(contentsOf: [tid, name], at: 0)
在 iCloudSchemaFieldValue
结构或 class 中添加一个初始值设定项,它采用 LoggerStartup.eTblTypes
类型并在 init 方法中设置适当的 属性
init(field: LoggerStartup.eTblTypes)
那你就可以写
var selectFields = [iCloudSchemaFieldValue]()
selectFields = [iCloudSchemaFieldValue(field: .tid),
iCloudSchemaFieldValue(field: .name)]
为此,您可以添加更新对象和 returns 本身的方法。
我们称它为 updating
.
extension iCloudSchemaFieldValue {
func updating(with closure: (iCloudSchemaFieldValue) -> Void) -> iCloudSchemaFieldValue {
closure(self)
return self
}
}
因此您的代码将如下所示:
let selectFields = [
iCloudSchemaFieldValue().updating(with: { [=11=].setField(LoggerStartup.eTblTypes.tid) }),
iCloudSchemaFieldValue().updating(with: { [=11=].setField(LoggerStartup.eTblTypes.name) })
]
是否有一些 shorthand 语法允许我添加一个对象并将其属性设置为一个数组。这就是我目前正在做的,但是我想将这些对象全部附加到一个附加语句中。
我尝试过各种语法,使用 self()
,但运气不佳。
var selectFields = [iCloudSchemaFieldValue]()
let tid = iCloudSchemaFieldValue()
tid.setField(LoggerStartup.eTblTypes.tid)
selectFields.append(tid)
let name = iCloudSchemaFieldValue()
name.setField(LoggerStartup.eTblTypes.name)
selectFields.append(name)
这是我的枚举
public enum eTblTypes
{
public static let tid = 2010
public static let name = 2020
//
}
这是我的 Objective C 框架 class..
@interface iCloudSchemaFieldValue : NSObject
@property (nonatomic) NSInteger enumVal;
@property (nonatomic) NSInteger tableEnumVal;
@property (nonatomic) id value;
@property (nonatomic) BOOL sysGenDefault;
@property (nonatomic) eOperator whereOperator;
@property (nonatomic) eOperator whereCondition;
@property (nonatomic) NSInteger padAmount;
-(id)setField: (NSInteger)ev;
-(id)setFieldVal: (NSInteger)ev
value: (id)value;
-(id)setFieldWithPadding: (NSInteger)ev
padding: (NSInteger)padding;
-(id)setFieldSysGenVal: (NSInteger)ev;
-(id)setWhereVal: (NSInteger)ev
table: (NSInteger)tableEv
operator: (eOperator)op
value: (id)value
condition: (eOperator)condition;
@end
我真的不想向这个 class 添加 init 方法,我有几个辅助方法,我想在其中使用相同的简写语法方法。
我不知道这样会不会好很多,但你可以这样做:
let selectFields: [iCloudSchemaFieldValue] = [
LoggerStartup.eTblTypes.tid,
LoggerStartup.eTblTypes.name,
... // add other fields you want to set here
].map {
let obj = iCloudSchemaFieldValue()
obj.setField([=10=])
return obj
}
我假设 LoggerStartup.eTblTypes.tid
和 LoggerStartup.eTblTypes.name
是同一类型 Int
,并且 iCloudSchemaFieldValue
是一个类型。
现在每次要设置一个新字段时,只需多添加一行,而不是 3 行。
如果您可以编辑 iCloudSchemaFieldValue
以添加采用 Int
的初始化程序,那将是最好的。然后你可以这样做:
let selectFields = [
LoggerStartup.eTblTypes.tid,
LoggerStartup.eTblTypes.name,
... // add other fields you want to set here
].map(iCloudSchemaFieldValue.init)
I'd like to append these objects all in one append statement.
你可以这样做:
selectFields = [tid, name]
但由于您的数据源 selectFields
已经初始化,这样就可以了:
selectFields.insert(contentsOf: [tid, name], at: 0)
在 iCloudSchemaFieldValue
结构或 class 中添加一个初始值设定项,它采用 LoggerStartup.eTblTypes
类型并在 init 方法中设置适当的 属性
init(field: LoggerStartup.eTblTypes)
那你就可以写
var selectFields = [iCloudSchemaFieldValue]()
selectFields = [iCloudSchemaFieldValue(field: .tid),
iCloudSchemaFieldValue(field: .name)]
为此,您可以添加更新对象和 returns 本身的方法。
我们称它为 updating
.
extension iCloudSchemaFieldValue {
func updating(with closure: (iCloudSchemaFieldValue) -> Void) -> iCloudSchemaFieldValue {
closure(self)
return self
}
}
因此您的代码将如下所示:
let selectFields = [
iCloudSchemaFieldValue().updating(with: { [=11=].setField(LoggerStartup.eTblTypes.tid) }),
iCloudSchemaFieldValue().updating(with: { [=11=].setField(LoggerStartup.eTblTypes.name) })
]