Swift: Array contains(AnyObject) error Cannot convert type of ... throw -> Bool - swift

Why I use Array.contains(AnyObject) to checking whether an structure object exists in this Array. It makes error : "Cannot convert type of ... throw -> Bool"
struct DecorationPatternsData {
let patternImageName: String
init(patternImageName: String) {
self.patternImageName = patternImageName
}
}
var decorationPatterns : [DecorationPatternsData] = [DecorationPatternsData(patternImageName: "decoration1.gif"), DecorationPatternsData(patternImageName: "decoration1.gif"), DecorationPatternsData(patternImageName: "decoration1.gif")]
var pickedDecorationPattern : DecorationPatternsData? = nil
...
if (pickedDecorationPattern != nil) {
if (decorationPatterns.contains(pickedDecorationPattern)) {
// Error: Cannot convert type of ... throw -> Bool
}
}

That's because your DecorationPatternsData doesn't conform to Equatable which is a requirement for contains(_:) to work.
Solution 1:
extension DecorationPatternsData: Equatable { }
func ==(lhs: DecorationPatternsData, rhs: DecorationPatternsData) -> Bool {
return lhs.patternImageName == rhs.patternImageName
}
Now DecorationPatternsData conforms to Equatable so you can use:
if let pickedDecorationPattern = pickedDecorationPattern {
if decorationPatterns.contains(pickedDecorationPattern) {
// Your code
}
}
Solution 2:
if decorationPatterns.contains({ $0 == pickedDecorationPattern }) {
// Your code
}
Here you're using a closure to compare the elements, returning a bool for contains(_:)

...
if let pickedDecorationPattern = pickedDecorationPattern {
if decorationPatterns.contains(pickedDecorationPattern) {
// Error: Cannot convert type of ... throw -> Bool
}
}

Related

Storing Generic Objects in a Heterogeneous Array and retrieving object parameter as the correct type

Ahoy everyone,
I have recently been trying to implement a Node based graph system that passes data between nodes using plugs. Similar to many of the 3D applications like houdini and maya.
I have written a similar system before using Python, and wanted to try this with Swift as my first learning excersise. Boy did I jump into the deep end on this one.
I am stuck now with Swifts Arrays, as I would like to store a list of Generic plugs.
Each plug can have its own value type float, int, color, string, Vector Matrix.
I have read up about Type Erasers and opaque types, but still cant seem to get my values our of a list in a way that I can perform some arithmetic on them.
All and any help that might put me in the direction would be greatly appreciated :D
import Foundation
import MetalKit
protocol genericPlug {
associatedtype T
func GetValue() -> T
}
class Plug<T>:genericPlug{
var _value:T?
var value:T {
get{GetValue()}
set(val){
value = val
}
}
func GetValue() -> T{
return _value!
}
init(_ newValue:T){
_value=newValue
}
}
class Node{
var plugs:[genericPlug] = []
init(){
var p1 = Plug<Int>(0)
var p2 = Plug(vector2(1.2, 3.1))
var p3 = Plug([0.0, 3.1, 0.6, 1])
plugs.append(p1)
plugs.append(p2)
plugs.append(p3)
}
func execute(){
// will access the plugs in the array and perform some sort of calculations on them.
plugs[0].value + 1 // should equal 1
plugs[1].value.x + 0.8 // should have x=2.0 y=3.1
plugs[2].value[1] - 0.1 // should equal 3.0
}
}
Thanks everyone
Use a generic something to extract what you need. Your options are methods and subscripts.
protocol PlugValue {
init()
}
extension Int: PlugValue { }
extension Float: PlugValue { }
extension Double: PlugValue { }
extension SIMD3: PlugValue where Scalar == Int32 { }
struct Plug<Value: PlugValue> {
var value: Value
init(_ value: Value) {
self.value = value
}
}
protocol AnyPlug {
var anyValue: PlugValue { get }
}
extension AnyPlug {
subscript<Value: PlugValue>(type: Value.Type = Value.self) -> Value {
anyValue as? Value ?? .init()
}
func callAsFunction<Value: PlugValue>(_ type: Value.Type = Value.self) -> Value {
anyValue as? Value ?? .init()
}
}
extension Plug: AnyPlug {
var anyValue: PlugValue { value }
}
let plugs: [AnyPlug] = [
Plug(1),
Plug(2.3 as Float),
Plug(4.5),
Plug([6, 7, 8] as SIMD3)
]
plugs[0][Int.self] // 1
plugs[1][Double.self] // 0
plugs[1][] as Float // 2.3
let double: Double = plugs[2]() // 4.5
plugs[3](SIMD3.self).y // 7
With the array of protocols, do you have to down cast them into their Plug when retrieving them every time?
Essentially. This is true of all heterogenous sequences. Here are your options:
extension Array: PlugValue where Element: PlugValue { }
let plug: AnyPlug = Plug([0.1, 1.1, 2.1])
(plug as? Plug<[Double]>)?.value[1]
(plug.anyValue as? [Double])?[1]
extension Plug {
enum Error: Swift.Error {
case typeMismatch
}
}
extension AnyPlug {
func callAsFunction<Value: PlugValue, Return>(_ closure: (Value) -> Return) throws {
guard let value = anyValue as? Value
else { throw Plug<Value>.Error.typeMismatch }
closure(value)
}
}
try plug { (doubles: [Double]) in doubles[1] } // 1.1
try plug { ($0 as [Double])[1] } // 1.1
try plug { $0 as Int } // <Swift.Int>.Error.typeMismatch
I managed to find a solution that worked for my needs.
I am still looking at finding a better way to handle getting the data and their correct type.
import Foundation
import MetalKit
// Creating the PlugType Enum
enum PlugType{
case Integer(Int?)
case Float_(Float?)
case Double_(Double?)
case Vector3(simd_int3)
// default types
static func IntegerType() -> PlugType{ return PlugType.Integer(nil)}
static func FloatType() -> PlugType{ return PlugType.Float_(nil)}
static func DoubleType() -> PlugType{ return PlugType.Double_(nil)}
}
// Implements a way to retrieve the correct value type
extension PlugType{
var IntegerValue: Int{
switch self{
case .Integer(let value):
return value ?? 0
default:
return 0
}
}
var FloatValue: Float{
switch self
{
case .Float_(let value):
return value ?? 0
default:
return 0
}
}
var DoubleValue: Double{
switch self
{
case .Double_(let value):
return value ?? 0
default:
return 0
}
}
}
// Get the string representation of the PlugType
extension PlugType {
var typeName: String{
switch self {
case .Integer: return "Integer"
case .Float_: return "Float"
case .Double_: return "Double"
case .Vector3: return "Vector3"
}
}
var swiftType: Any.Type {
switch self {
case .Integer: return Int.self
case .Float_: return Float.self
case .Double_: return Double.self
case .Vector3: return simd_int3.self
}
}
}
class Plug{
var _value:PlugType?
var type:String? { get{ return _value?.typeName } }
init(_ newValue:PlugType){
_value = newValue
}
func geee<T>(_ input:T) -> T{
switch type {
case "Integer":
return getVal(_value!.IntegerValue) as! T
case "Double":
return getVal(_value!.DoubleValue) as! T
default:
return 0 as! T
}
}
func getVal(_ val:Int) -> Int {
return val
}
func getVal(_ val:Float) -> Float {
return val
}
func getVal(_ val:Double) -> Double {
return val
}
}
var plugs:[Plug] = []
var p1 = Plug(PlugType.Integer(2))

Why can't I get type-specific information in my Sequence extension?

Why does this work, where PageBreak is a NSManagedObject
extension Sequence where Iterator.Element : PageBreak {
var landscape: [PageBreak] {
return self.filter({ (pageBreak) -> Bool in
return !pageBreak.isPortraitOrientation
})
}
var portrait: [PageBreak] {
return self.filter({ (pageBreak) -> Bool in
return pageBreak.isPortraitOrientation
})
}
}
But not this:
extension Sequence where Iterator.Element : String {
var onlyDumbOnes: [String] {
return self.filter({ (string) -> Bool in
if string.hasPrefix("Dumb") {
return true
}
return false
})
}
}
The compiler fails.
Value of type 'Self.Iterator.Element' has no member 'hasPrefix'
So it doesn't seem to know that we're dealing with String objects.
String is a struct and not a class or a protocol, so the syntax Iterator.Element : String doesn't make sense since Iterator.Element can't be a subclass of String or implement the protocol String. Instead, use Iterator.Element == String:
extension Sequence where Iterator.Element == String {
var onlyDumbOnes: [String] {
return self.filter { (string) -> Bool in
if string.hasPrefix("Dumb") {
return true
}
return false
}
}
}

Trying to get generic code from working Iterator pattern

Here is the code that works fine and this is implementation of the Iterator pattern:
struct Candies {
let candies: [String]
}
extension Candies: Sequence {
func makeIterator() -> CandiesIterator {
return CandiesIterator(sequence: candies, current: 0)
}
}
struct CandiesIterator: IteratorProtocol {
let sequence: [String]
var current = 0
mutating func next() -> String? {
defer { current += 1 }
return sequence.count > current ? sequence[current] : nil
}
}
Here is the code that I thought to be as a generic variation of the code above but I have two errors (see below the code):
struct Whatevers<T> {
let whatevers: [T]
}
extension Whatevers: Sequence {
func makeIterator() -> Whatevers<T>.Iterator {
return WhateversIterator(sequence: whatevers, current: 0)
}
}
struct WhateversIterator<T>: IteratorProtocol {
let sequence: [T]
var current = 0
mutating func next() -> WhateversIterator.Element? {
defer { current += 1 }
return sequence.count > current ? sequence[current] : nil
}
}
error: MyPlayground.playground:854:1: error: type 'Whatevers' does
not conform to protocol 'Sequence' extension Whatevers: Sequence { ^
error: MyPlayground.playground:861:8: error: type
'WhateversIterator' does not conform to protocol 'IteratorProtocol'
struct WhateversIterator: IteratorProtocol {
Can someone explain what is incorrect in this code. And how can I make it work?
Solution found!
struct Whatevers<T> {
let whatevers: [T]
}
extension Whatevers: Sequence {
func makeIterator() -> WhateversIterator<T> {
return WhateversIterator(sequence: whatevers, current: 0)
}
}
struct WhateversIterator<T>: IteratorProtocol {
let sequence: [T]
var current = 0
mutating func next() -> T? {
defer { current += 1 }
return sequence.count > current ? sequence[current] : nil
}
}
All the mistakes were about returning types from functions makeIterator and next.
Hope somebody will find it helpful!

Swift 3 to Swift 4 kishikawakatsumi/SpreadsheetView

I'm using a third party library and I'm getting errors which I don't understand (I'm new to Swift).
Here's the code:
final class ReusableCollection<Reusable>: Sequence where Reusable: NSObject {
var pairs = [Address: Reusable]()
var addresses = Set<Address>()
var objects: LazyMapCollection<[Address: Reusable], Reusable> {
return pairs.values
}
func contains(_ member: Address) -> Bool {
return addresses.contains(member)
}
#discardableResult
func insert(_ newMember: Address) -> (inserted: Bool, memberAfterInsert: Address) {
return addresses.insert(newMember)
}
func subtract(_ other: Set<Address>) {
addresses.subtract(other)
}
subscript(key: Address) -> Reusable? {
get {
return pairs[key]
}
set(newValue) {
pairs[key] = newValue
}
}
func makeIterator() -> LazyMapIterator<DictionaryIterator<Address, Reusable>, Reusable> {
return pairs.values.makeIterator()
}
On the return pairs.values I'm getting this error:
"Cannot convert return expression of type 'Dictionary.Values' to return type 'LazyMapCollection<[Address : Reusable], Reusable>'"
And on the last line I'm getting this error:
"Ambiguous reference to member 'makeIterator()'"
Further more I have this:
let mergedCells = dataSource.mergedCells(in: self)
let mergedCellLayouts: [Location: CellRange] = { _ in
var layouts = [Location: CellRange]()
And here I'm getting this error:
"Cannot convert value of type '(_) -> _' to specified type '[Location : CellRange]'"
Any idea? I've made some research but I haven't been able to solve this.
This problem was fixed at 0.7.3. Please use the latest version.

Extending SignalProducerType in case Value is an Array<SomeProtocol>

I have a protocol for fetching database objects by PrimaryKey
typealias PrimaryKey = String
protocol PrimaryKeyConvertible {
var pkValue : PrimaryKey { get }
static func pkObject(key: PrimaryKey) -> Self?
}
and I want to extend the SignalProducerType to be able to operate on a SignalProducer.Value of that type.
So the Single object extension (single as in not Array) works fine and implemented as following:
extension SignalProducerType
where Value: PrimaryKeyConvertible
{
func fetchOnMainThread() -> SignalProducer<Value?, Error> {
return
self.map{ (obj: Value) -> PrimaryKey in
return obj.pkValue
}
.observeOn(UIScheduler())
.map{ (key: PrimaryKey) -> Value? in
return Value.pkObject(key)
}
}
}
But when I try to implement it on an Array of these elements i hit some compilation challenges:
extension SignalProducerType
{
func fetchOnMainThread<P: PrimaryKeyConvertible where Self.Value == Array<P>>() -> SignalProducer<[P], Error> { //(1)
return self.map({ (value: Self.Value) -> [PrimaryKey] in
return value.map{ $0.pkValue } //(2)
})
}
}
(1) i suspect that the signature is not communicating the idea to the compiler correctly
(2) produces the following error:
Type of expression is ambiguous without more context
the issue i'm trying to solve is how to let the compiler recognize the the SignalProducer is operating on an Array<P> where P is PrimaryKeyConvertible and have the .map operate on it accordingly ...
my current solution for the array issue is to implement using a generic function as listed below:
func fetchOnMainThread<Value: PrimaryKeyConvertible, Error: ErrorType>
(signal: SignalProducer<[Value], Error>) -> SignalProducer<[Value], Error> {
return signal
.map{ (convertibles: [Value]) -> [PrimaryKey] in
return convertibles.map { $0.pkValue }
}
.observeOn(UIScheduler())
.map{ (keys: [PrimaryKey]) -> [Value] in
return keys.flatMap{ Value.pkObject($0) }
}
}
and then used for example:
extension GoogleContact: PrimaryKeyConvertible {...}
extension GoogleContact {
static func fetchGoogleContactsSignal() -> SignalProducer<[GoogleContact], GoogleContactError> { ...}
}
and the call site would be like:
let signal = fetchOnMainThread(GoogleContacts.fetchGoogleContactsSignal()).onNext...
where I would prefer to have it as an extension where it would flow as usual
GoogleContacts
.fetchGoogleContactsSignal()
.fetchOnMainThread()
Update
another version of the function I've tried : (#J.Wang)
extension SignalProducerType
where Value == [PrimaryKeyConvertible]
{
func fetchArrayOnMainThread2<T: PrimaryKeyConvertible>() -> SignalProducer<[T], Error> {
return self
.map{ (values: Self.Value) -> [PrimaryKey] in
return values.map{ $0.pkValue }
}
.deliverOnMainThread()
.map{ (keys: [PrimaryKey]) -> [T] in
return keys.flatMap{ T.pkObject($0) }
}
}
}
let signal =
GoogleContacts
.fetchGoogleContactsSignal()
.fetchArrayOnMainThread2() //(3)
(3) Generates error:
'[PrimaryKeyConvertible]' is not convertible to '[GoogleContact]'
Hmm, although I'm not quite sure what the problem is, but I think the following implementation might be what you want.
extension SignalProducerType where Value == [PrimaryKeyConvertible]
{
func fetchOnMainThread() -> SignalProducer<[PrimaryKey], Error> {
return self.map { value in
value.map { $0.pkValue }
}
}
}
Try this:
extension SignalProducerType where Value == [PrimaryKeyConvertible]
{
func fetchOnMainThread<T: PrimaryKeyConvertible>() -> SignalProducer<[T], Error> {
return self.map { value in
value.map { $0.pkValue }
}.map { keys in
keys.flatMap { T.pkObject($0) }
}
}
}