I have a class:
This class is to forge an array and also provides an inheritance to NSObject.
When I try to use it in objective-c code. It seems the conversion failed. After experiment a little while , I found after removing all <T>s and replace T with NSObject makes it OK.
I've already written lots of classes based on ArrayLike and I'm eagering to use these classes in obj-c. Is there any workaround for this situation?
public class ArrayLike<T>: NSObject, SequenceType{
//
public typealias SubSequence = Array<T>.SubSequence
public typealias Generator = Array<T>.Generator
public func generate() -> ArrayLike.Generator {
return _impl!.generate()
}
public func underestimateCount() -> Int {
return _impl!.underestimateCount()
}
public func map<T>(@noescape transform: (ArrayLike.Generator.Element) throws -> T) rethrows -> [T] {
return try _impl!.map(transform)
}
public func filter(includeElement: (ArrayLike.Generator.Element) throws -> Bool) rethrows -> [ArrayLike.Generator.Element] {
return try _impl!.filter(includeElement)
}
public func forEach(body: (ArrayLike.Generator.Element) throws -> ()) rethrows {
return try _impl!.forEach(body)
}
public func dropFirst(n: Int) -> ArrayLike.SubSequence {
return _impl!.dropFirst(n)
}
public func dropLast(n: Int) -> ArrayLike.SubSequence {
return _impl!.dropLast(n)
}
public func prefix(maxLength: Int) -> ArrayLike.SubSequence {
return _impl!.prefix(maxLength)
}
public func suffix(maxLength: Int) -> ArrayLike.SubSequence {
return _impl!.suffix(maxLength)
}
public func split(maxSplit: Int, allowEmptySlices: Bool, @noescape isSeparator: (ArrayLike.Generator.Element) throws -> Bool) rethrows -> [ArrayLike.SubSequence] {
return try _impl!.split(maxSplit, allowEmptySlices: allowEmptySlices, isSeparator: isSeparator)
}
//
public override init() {
super.init()
_impl = [T]()
}
public init(count: Int, repeatedValue: T) {
super.init()
_impl = [T](count: count, repeatedValue: repeatedValue)
}
public convenience init(wrapSingle single:T) {
self.init()
_impl!.append(single)
}
public init<S: SequenceType where S.Generator.Element == T>(other o: S) {
super.init()
_impl = [T](o)
}
//
public subscript(index: Int) -> T {
set {
_impl![index] = newValue
}
get {
return _impl![index]
}
}
//
public var count: Int {
get {
guard _impl != nil else {
return 0
}
return _impl!.count
}
}
public var last: T? {
get {
guard _impl!.count > 0 else {
return nil
}
return _impl![_impl!.count - 1]
}
}
//
public func append(newElement: T) {
_impl!.append(newElement)
}
public func appendContentsOf<S: SequenceType where S.Generator.Element == T>(collection: S) {
_impl!.appendContentsOf(collection)
}
public func appendContentsOf<S: CollectionType where S.Generator.Element == T>(sequence: S) {
_impl!.appendContentsOf(sequence)
}
public func insert(newElement: T, atIndex: Int) {
_impl!.insert(newElement, atIndex:atIndex)
}
//
public func removeAll() {
_impl!.removeAll()
}
public func removeAtIndex(index: Int) {
_impl!.removeAtIndex(index)
}
public func removeFirst() {
_impl!.removeFirst()
}
public func removeFirst(n: Int) {
_impl!.removeFirst(n)
}
public func removeLast() {
_impl!.removeLast()
}
public func removeRange(range: Range<Int>) {
_impl!.removeRange(range)
}
//
public func minimumValue(lessCompare: (T,T) -> Bool) -> T? {
guard count > 0 else {
return nil
}
var result = self[0]
for i in 1..<count {
if lessCompare(self[i], result) {
result = self[i]
}
}
return result
}
//
public func maximumValue(lessCompare: (T,T) -> Bool) -> T? {
guard count > 0 else {
return nil
}
var result = self[0]
for i in 1..<count {
if lessCompare(result, self[i]) {
result = self[i]
}
}
return result
}
public func subArray(range: Range<Int>) -> ArraySlice<T> {
var result = ArraySlice<T>()
for i in range.startIndex...range.endIndex {
result.append(self[i])
}
return result
}
public var isEmpty: Bool {
return count == 0
}
//
private var _impl: Array<T>? = nil
}