Working with Any and AnyObject in Swift

Hi, I am working with an iOS app and just started to use Silver and I found few basic things not working:

var rect = CGRect(x:0, y:0, width:0, height:0) // throws no matching overload compile error

//workaround:
var rect = CGRect()
rect.origin.x = 0
rect.origin.y = 0
rect.size.width = 0
rect.size.height = 0

var classStore = [String:Any]()
classStore["name"] = rect // Error: parameter 2 should be Any?
classStore["name"] = rect as! Any  // Error: invalid cast

Apple swift don’t have any problems with the above code

classStore["name"] = CGRect() as! Any // This works
classStore["name"] = CGRect()  // Error: Parameter 2 should be "id?"
classStore["name"] = CGRectMake(0, 0, 0, 0)  // Error: Parameter 2 should be "id?"
classStore["name"] = CGRectMake(0, 0, 0, 0) as! Any // Invalid cast

But I am not able to see the available methods on CGRect, Silver say everything that on iOS libraries are available then why am I not allowed to use the below.

let rect = CGRect(x:0, y:0, width:0, height:0) // Error: no matching overload

is there anyway I can browse the headers of .fx files as Apple and Silver are not agree here…

Those do sound like bugs, yes. i’ll log an issue.

Thanks, logged as bugs://72584

While developing in Fire, I am not getting any clue about method signatures (no auto complete tips / hints ). Its like a person walking on the road at night without street lights.

Even though silver states .fx files are identical to iOS SDK framework, not able to browse API’s of .fx is really frustrating as Users gets confused whether its their mistake, or Elements.

I am not able to find the FXGen tool with recent Fire distribution, can someone point from where I can download it.
I highly appreciate if someone point me to any docs or link for Browsing .fx framework API’s

I don’t have any problem in working in Silver Swift way but without knowing what compiler expects when it throw errors like the one in this thread really worries me.

Hmm, you’re not getting any code completion dropdown, anywhere? even if you manually invoke it via Escape? That’s not how it should be, something is broken.

i’m afraid those errors are a compiler bug, pure and simple. We’ll get those fixed for one of the next beta drops.

I downloaded silver 4 days back,
My System: Mac-mini (Late 2012) with OS X 10.10.3

Fire Info:
Fire: 8.1.85.1801 built 07-14-2005
Internal Mono: 3.10.0 (64 bit)
Internal Elements: 8.1.85.1801
Cocoa SDKs: 11 SDKs
Java SDK: 1.8.0_25-b17
Android SDK: Latest Installed

Sorry, I think CC is working for most part, but for CG (CGRect, CGPoint ect) types it needs to improve I think.
I am really want to use silver for my current project as I am comfortable using swift.

But after working with couple of hours, and when I see CGRect which is very basic for UI design not working I am not sure whether to go ahead but I am kind of desperate to use silver that the reason I have posted the issue here.
To be fare to Silver, it recommends using respective target design editors for developing UI, but I do all UI in code.
So this might be a reason that silver has some issue like the one I post here.

I actually started to convert my existing Apple swift code to silver, and In the process I have encountered too many error due to silver swift limitation of computed properties in an extension.
Most of the CG* type errors are related due to this.

The issues I have reported are resolved by implementing all unresolved CG* type computed properties to extension methods.

As far as this thread is concerned, the support team can close the issue.

My existing Apple swift code contains too many computed properties in extensions, right now I am converting those to functions, In the mean time I want to know from the silver team about the performance implications.

In general I don’t feel this will impact the performance, but I want to know silver team advice on this as my code is going to be used as a base foundation framework.

computed properties in extensions should be fine. you just cannot add stored properties — i.e. you cannot add new actual data to an existing class/struct. If you see errors added a computed property,can you send me a tescase?

that it wont. methods and computed properties are virtually the same thing.

can you give me an example 9say with screenshot) with whats wrong with CC on CGRect & Co? they work fine for me here. e.g.: Image 2015-07-26 at 12.36.4...

Note that these are Cocoa/Objective-C structs. the don’t have many members.

—marc

Ex:
One of my file contains references to below CGRect computed properties
CGRect.contain(CGPoint)
CGRect.minX
CGRect.maxX
CGRect.minY
CGRect.maxY
CGRect.width
CGRect.height

Now I understood why I am not getting the above methods.

Why from Objective-C structs?, Is there any specific reason.
I have converted by Initial Obj-c code to swift, Now I have change these parts to Obj-c

Anyway thanks for reminding my ignorance of Obj-c methods.

Silver knows nothing abut Apple Swift, really. Like our C# and Oxygene languages, it compiles against the native Cocoa APIs.

I suppose it would make sense to add common stuff such as these extensions to libSwift. You feel like contributing that? GitHub - remobjects/SwiftBaseLibrary.

fatalError shold be defined libSwift already. the other errors in your screenshots all make sense. for example there’s no width, in 'bounds, you’ll need bounds.size.width?

import Swift has no effect, means ‘fatalError’ is not defined in libSwift!!

Hmm, i see it, in Functions.swift. can you send me your project? do you have libSwift referenced?

ah damn, nevermind. it’s missing “public”. fixed for the next beta.

computed properties in extensions should be fine. you just cannot add stored properties — i.e. you cannot add new actual data to an existing class/struct. If you see errors added a computed property,can you send me a tescase?

Test Cases:

extension CGFloat {
	var alignPixel:CGFloat = { return ceil(self * scale) / scale }
	var lineOffset:CGFloat = { return floor(self * scale) / scale }
}  //  Err: Type mismatch, expected 'CGFloat'

extension CGFloat {
    	var alignPixel:CGFloat = { return CGFloat(ceil(self * scale) / scale) }
    	var lineOffset:CGFloat = { return CGFloat(floor(self * scale) / scale) }
    }  //  Err: Type mismatch, expected 'CGFloat'

extension CGFloat {
	var alignPixel = { return CGFloat(ceil(self * scale) / scale) }
	var lineOffset = { return CGFloat(floor(self * scale) / scale) }
} //  Err: Type mismatch

That’s not a computed property?

var alignPixel:CGFloat { get { return ceil(self * scale) / scale } }

would be. You’re assigning a closure to a CGFloat, hence the (correct) error.

var alignPixel:CGFloat = { return ceil(self * scale) / scale }()

(note the extra () would be valid, but also to a computed property, but a stored one, initialized by the closure. (the () calls the closure,and returns the result, without the () you’re assigning the closure itself.

Thanks, I knew I am assigning a closure, but the apple swift book, shows this as an example for computed properties in an extension, so I thought compiler will does its magic.

Thanks you for saving me, from going down the wrong path!!

No its was my mistake,
Apple Docs:

extension Double {
    var km: Double { return self * 1_000.0 }
}

I just overlooked at the syntax!!

1 Like

After making the changes, it worked, Thanks again for saving my time!!

extension CGFloat {
    var alignToPixel:CGFloat { 
        let scale = UIScreen.mainScreen().scale
        return ceil(self * scale) / scale 
    }
    var lineOffset: CGFloat {
        let scale = UIScreen.mainScreen().scale
        return (scale % 2 == 0) ? self+0.25 : self+0.5
    }
}
1 Like