gpt4 book ai didi

arrays - 仅针对具有 objective-c 元素的数组的 Swift 扩展可能吗?

转载 作者:可可西里 更新时间:2023-11-01 01:56:41 26 4
gpt4 key购买 nike

大家好,我只想为具有 objective-c 元素的数组添加 Array swift 扩展,这可能吗?

例如:

extension Array where Element:ObjectiveCClass{

func unionWithoutDuplicatesPropertiesWithArray(array:[Element] ,key:String) -> [Element]{
var arrayA:[Element]
var arrayB:[Element]

if self.count > array.count {
arrayA = self;
arrayB = array;
} else {
arrayA = array;
arrayB = self;
}

for object in arrayA
{
if object.valueForKey(key)
{
//do something
}
}
return arrayA
}
}

最佳答案

试试NSObject

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.

extension Array where Element: NSObject{

func unionWithoutDuplicatesPropertiesWithArray(array:[Element] ,key:String) -> [Element]{
var arrayA:[Element]
var arrayB:[Element]

if self.count > array.count {
arrayA = self;
arrayB = array;
} else {
arrayA = array;
arrayB = self;
}

for object in arrayA
{
if object.value(forKey: key) != nil
{
//do something
}
}
return arrayA
}
}

请注意,您没有使用 arrayB 的值。

关于arrays - 仅针对具有 objective-c 元素的数组的 Swift 扩展可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52753151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com