Reputation:
I was setting up BLKFlexibleHeightBar with swift. while I was writing var flexibleHeightBar = BLKFlexibleHeightBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 100))
, an error as the title occurred.
How can I solve this problem?
Upvotes: 0
Views: 1029
Reputation: 32681
The error is obviously related to your call to self.view
, where, according to the error, the compiler thinks self
is of type ViewController -> () -> ViewController
, instead of what you probably expect it to be ViewController
.
Where did you write this code?
If you wrote it in some init
code or in a static func
of your ViewController
, then in that context self
is not an instance of ViewController
but something else. And given where you wrote that code, self.view
does not represent you ViewController
's instance's view
property, probably because self
isn't created yet.
You need to move your code from init()
(where the self
instance is being created but not ready yet) to viewDidLoad
(once the ViewController
's view
was just created).
ViewController -> () -> ViewController
typeIf you want to get a bit more technical and understand why the compiler expect self
to be of that type in the error you had, know that in Swift, an instance method is actually also exposed as a class method which takes an instance as parameter and return the instance method (that mechanism is called "currying").
For example the String
class has an instance method hasPrefix
whose type is String -> Bool
. It is also exposed as a class method that expect a String
instance and return that aforementioned instance method applied on that instance; so that String.hasPrefix
is of type String -> String -> Bool
, and String.hasPrefix("Hello world")("Hello")
is strictly equivalent to "Hello world".hasPrefix("Hello")
(yeah I know that can seems strange at first, but it makes sense). Of course in general you rarely (if ever) use this in your daily code, that's just a rarely used feature of Swift, but it's there.
Back to your context, you were in the init
method of your ViewController
. init()
is in fact a (special) instance method (no static
/class
before init
) that, in that case, takes no parameter (()
) and will return a ViewController
instance, so its type is () -> ViewController
. This instance method can also be interpreted as a class method (on the ViewController
class) whose type is ViewController -> () -> ViewController
, because behind the scenes it will be called with an (uninitialized, freshly allocated) instance of ViewController
and will return a function (init
) that, once called (with no parameter, ()
), will return the initialized instance of ViewController
.
So that's where that ViewController -> () -> ViewController
comes from in your error message above.
Upvotes: 1
Reputation: 534950
The frame
is supposed to be a CGRect, and you are not providing one. Your frame: 0, 0, self.view.frame.size.width, 100
is just a series of numbers, not a CGRect. Swift has no idea what all those numbers are doing there. If you want a CGRect - and you do - you must say so:
frame: CGRectMake(0, 0, self.view.frame.size.width, 100)
Upvotes: 3