Is there a way to simplify this into one line in Swift 2.0?
enum Direction {
case Up
case Down
}
var panDirection: Direction?
Something like this, which doesn't work:
var panDirection = enum Direction {
case Up
case Down
}
Even if you could do it, those are not the same at all. enum is an object type, like class. In your first example, panDirection is an instance of the Direction enum. In your second example, if it could compile and run, panDirection would end up as the enum itself (the type, not an instance of the type) — which is not at all what you want.
Thus, what you are trying to do is to declare a type in the middle of a line. You can't do that. The rules for where you can declare a type are very clear and very strict.
Note, however, that you can declare a type within another type, or even purely locally, e.g. within a function's code. Thus, for example, you can declare the type temporarily as a way of passing data around inside a function. Nutty but legal:
func myCoolFunction(up:Bool) {
enum Direction : String {
case Up
case Down
}
let dir : Direction = (up ? .Up : .Down)
print("user wants \(dir)")
}
No, an enum type must be declared separately before it can be used as a variable's type.
Related
I need to know if Swift language has some way to achieve this feature of marking some elements of an enum. For instance, I have a set of properties in Swift enum and some of the properties are animatable while others are not. I want something like:
public enum Property: String {
case prop1,
case prop2 #animatable,
case prop3 #animatable,
case prop4,
case prop5 #animatable,
...
...
...
case prop100
}
And then in a function I can pass an argument such as:
func animate(_ prop: Property #animatable, startTime:CFTimeInterval, duration:CFTimeInterval) {
}
So I pass a property which are elements of enum that are only animatable, not others. Is there a clean way to achieve something like this in Swift language (Swift 5)?
Alright, I've done my homework and read what I could find but I can't seem to find if this is possible to accomplish with Swift. I've got an enum that I use all over the place: SomeEnum and one if it's cases is a lengthy SomeEnum.SomeLengthyCaseName and I'm tired of seeing it all over my code. I don't want to refactor because I like the descriptive nature of the case for when newcomer's maintain my code.
So here's the question: Is it possible to create a typealias for SomeEnum.SomeLengthyCaseName? And if so, how? Here's what I've tried:
enum SomeEnum {
case SomeLengthyCaseName
}
typealias SLCN = SomeEnum.SomeLengthyCaseName
That's the syntax but Xcode gives a compiler error saying that "SomeLenghtyCaseName is not a member of SomeEnum."
Ready, set, go!
That's a misleading error message.
The real problem is that SomeLengthyCaseName is not a type. Therefore you can't use typealias, which is only for aliases of types. (For example, you could say typealias SE = SomeEnum.)
Instead, you can just use a global constant:
let SLCN = SomeEnum.SomeLengthyCaseName
Or, better, a static constant on the enum itself:
enum SomeEnum {
case SomeLengthyCaseName
static let SLCN = SomeEnum.SomeLengthyCaseName
}
let x: SomeEnum = .SLCN
It's not possible since SomeEnum.SomeLengthyCaseName is not a type, it's a value for the SomeEnum type.
I'm having trouble grasping the proper way of instantiating variables that always need to be set before an object is fully functional but may need to be instantiated after the constructor. Based on Swift's other conventions and restrictions it seems like there is a design pattern I'm unaware of.
Here is my use case:
I have a class that inherits from UIViewController and will programmatically create views based on user actions
I need to attach these views to this class, but to do so I need to retrieve their content based on configuration data supplied by another controller
I don't care if this configuration data is passed to the constructor (in which case it would always be required) or supplied by a secondary call to this object before it is used
My problem seems to be that both of the approaches in bullet 3 seem flawed.
In the first case, there is only one legitimate constructor this class can be called with, yet I'm forced to override other constructors and initialize member variables with fake values even if the other constructors are never intended to be used (I'm also trying to keep these variables as let types based on Swift's best practices).
In the second case, I'm effectively splitting my constructor into two parts and introduce an additional point of failure in case the second part fails to be called prior to class being used. I also can't move this second part to a method that's guaranteed to be called prior to usage (such as viewDidLoad) because I still need to pass in additional arguments from the config. While I can make sure to call the initPartTwo manually, I'd prefer to have a mechanism that better groups it with the actual constructor. I can't be the first one to run into this and it seems like there is a pattern I'm not seeing to make this cleaner.
UPDATE:
I ended up going with a modified version of the pattern matt suggested:
struct Thing {
let item1: String
let item2: String
struct Config {
let item3: String
let item4: String
}
var config:Config! {
willSet {
if self.config != nil {
fatalError("tried to initialize config twice")
}
}
}
init() {
self.item1 = ...
self.item2 = ...
...
}
public func phaseTwoInit(item3: String, item4: String) {
self.item3 = item3
self.item4 = item4
...
}
}
var t = Thing()
...
t.phaseTwoInit(...)
...
// start using t
If an initial instance variable property value can't be supplied at object initialization time, the usual thing is to declare it as an Optional. That way it doesn't need to be initialized by the class's initializers (it has a value - it is nil automatically), plus your code subsequently can distinguished uninitialized (nil) from initialized (not nil).
If the Optional if an implicitly unwrapped Optional, this arrangement need have no particular effect on your code (i.e. it won't have to be peppered with unwrappings).
If your objection is that you are forced to open the door to multiple settings of this instance variable because now it must be declared with var, then close the door with a setter observer:
struct Thing {
var name:String! {
willSet {
if self.name != nil {
fatalError("tried to set name twice")
}
}
}
}
var t = Thing()
t.name = "Matt" // no problem
t.name = "Rumplestiltskin" // crash
I want to run different functions depending on selected level Integer
so if selected level is 1 then runfunc1(), if 2 then runfunc2()...
I know this is possible using if else
if levelselected == 1 {
runfunc1()
} else if levelseletecd == 2 {
runfunc2()
// ... and so on
}
Is there any better way than this, perhaps something like this
runfunc%i(),levelselected // I know its not correct but something similar
I dont want to write new code for every level, so any better way?
You can use something like:
var levelSelected = 0 //
var selector = Selector("runFunc\(levelSelected)")
if self.respondsToSelector(selector) {
NSThread.detachNewThreadSelector(selector, toTarget: self, withObject: nil)
}
You could have an array or dictionary of functions. A dictionary might be nicer since the logic for checking if the level is valid is a lot simpler:
let funcs = [1: runfunc1, 2: runfunc2]
if let funcToRun = funcs[levelselected] {
funcToRun()
}
However, you won't be able to easily dynamically build a function name from strings and numbers without using #objc functionality.
(except in the sense that you could make the key to the dictionary a string of the function name, but you still have to build the dictionary using actual function names determined at compile time)
That said, you can add to the funcs variable from elsewhere in the code so it does mean to can "hook up" new levels without changing this dispatching logic.
Not the exact solution you are looking for but this can make it easier :
Declare an array of the desired functions:
var levelFunctions: [()->()] = [runfunc1, runfunc2, runfunc3]
This syntax declares an array of functions that have zero argument and return nothing. You initialize this array with the required function names and then execute the desired function using the levelselected variable:
levelFunctions[levelselected]() // Or levelselected-1 if the variable is not zero-based
EDIT:
As Airspeed Velocity mentioned in the comment and his answer you should make sure the level is in the array bounds.
I prefer to create a function, for example runFuncFromLevel::Int -> (() -> Void). runFuncFromLevel return a proper function that you need.
func runFuncFromLevel(level: Int) -> () -> Void
{
switch level
{
case 1: return runfunc1
case 2: return runfunc2
default: return {}
}
}
ok so I am trying to return nil if a certain type is passed into my function. In this case im passing in an instance of my class "BlogPost" and a type within this blogpost. I also have an array called "types" and I have assigned the variable Videos to the last index of that array. If this type is passed into my function I would like to return nil (so assuming im going to need an optional here for returning a possible nil) this is what I have so far :-
so all in all I need to pass in an instance of my blog post but always return nil if a certain type is passed in. Hope this makes sense
Update:
The types array is defined as follows:
let types : [String] = ["technology", "Fashion", "Animals"]
this is the array I am referring to in the function. Basically if that last entry of the array is entered into the function I need to return nil
sure this is blogpost it does actually have an empty string for type
great so im getting there what Ive done now is change the blogpost.type to choose one at random. So now if the specfic type is chosen from this array how would I do that still getting an error. This is what I have updated to
so now all I need to do is access the 2 type in that array and if I do access it return nil. Any thoughts on that? so to drag it on thanks
I don't think you can. You can create failable initialisers which does what you need but you cannot use it with normal function.
The best solution for you would be return optional Int or String and when you call the function just check the result for nil and do what you need to do, otherwise ignore it:
func randomViews(blog : BlogPost.Type) -> Int? {
case 10:
return nil
case 10, 20 :
return 0
default:
random
}
if (randomViews(parameter) == nil) {
//function returned nil
}
You have displayed error because you compare optional blog to Videos, you have to unwrap it first, for example if you are sure the blog has always have a value use:
if blog! == Videos
if not sure is safer to use:
if let blg = blog {
if blg == Videos {
}
else {
// blog has not have a value
}
You are passing blog as a BlogPost.Type parameter. That is not correct. You should have either just passed it the String parameter, or you could pass it the BlogPost itself:
func randomViews(blog: BlogPost) {
let videos = types[2]
if blog.type == videos {
// do whatever you want
}
// carry on
}
Unrelated to your question at hand, but notice that I use let instead of var when defining videos. Always use let if the value will not (and cannot) change.
Also note that I use lowercase letter v in videos, because Cocoa naming conventions dictate that variables generally start with lowercase letters, whereas types, classes, structs, and enums generally start with uppercase letters.