I have an enum declaration.
enum OP_CODE {
case addition
case substraction
case multiplication
case division
}
And use it in a method:
func performOperation(operation: OP_CODE) {
}
We all know that how we can call this normally
self.performOperation(OP_CODE.addition)
But if I have to call it in some delegate where the integer value is not predictable, how do I call it?
For example:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.delegate.performOperation(indexPath.row)
}
Here, the compiler throws an error Int is not convertible to 'OP_CODE'. I've tried many permutations, but haven't been able to figure it out.
You need to specify the raw type of the enumeration
enum OP_CODE: Int {
case addition, substraction, multiplication, division
}
addition will have a raw value of 0, substraction of 1, and so on.
and then you can do
if let code = OP_CODE(rawValue: indexPath.row) {
self.delegate.performOperation(code)
} else {
// invalid code
}
More info here: https://developer.apple.com/library/ios/documentation/swift/conceptual/Swift_Programming_Language/Enumerations.html#//apple_ref/doc/uid/TP40014097-CH12-XID_222
for older swift releases
In case you're using an older version of swift, raw enumerations work a bit different. In Xcode < 6.1, you have to use fromRaw() instead of a failable initializer:
let code = OP_CODE.fromRaw(indexPath.row)
You can use raw values in your enum:
enum OP_CODE : Int{
case addition = 0
case substraction = 1
case multiplication = 2
case division = 3
}
and use the failable initializer taking a raw value as input:
let code = OP_CODE(rawValue: 2) // code == .multiplication
Note that code is an optional, because if the raw value doesn't map to a valid enum, the initializer returns nil.
In your case:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let code = OP_CODE(rawValue: indexPath.row)
if let code = code {
self.delegate.performOperation(code)
}
}
Moreover, given an instance of the enum, you can obtain the associated raw value using the rawValue property.
Note: enums have changed a little bit in Xcode 6.1 - if you're using a previous version, read #GabrielePetronella's answer and related comments.
Related
I'm faced with the problem of using generic class and inheritance.
Brief description of the problem:
I have a base class called BookPageDataSource and two inherited classes (ReadingBookPageDataSource and StarsBookPageDataSource) with different implementations.
Also, I have a generic class BookPageViewController that contains the generic parameter of this data source and two inherited classes (ReadingBookPageViewController and StarsBookPageViewController) from this class.
I need to write a method the return parameter of which is BookPageViewController<DataSource>.
// Data Sources
class BookPageDataSource { }
class ReadingBookPageDataSource: BookPageDataSource { }
class StarsBookPageDataSource: BookPageDataSource { }
// Controllers
class BookPageViewController<DataSource: BookPageDataSource>: UIViewController {
let dataSource: DataSource
init(dataSource: DataSource) {
self.dataSource = dataSource
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
return nil
}
}
final class ReadingBookPageViewController: BookPageViewController<ReadingBookPageDataSource> { }
final class StarsBookPageViewController: BookPageViewController<StarsBookPageDataSource> { }
// Communication
class Pager {
func currentPageController<DataSource>(at index: Int) -> BookPageViewController<DataSource> {
// for example
if index == 0 {
// How to remove the cast from the line below?
return readingPageController() as! BookPageViewController<DataSource>
}
return starsPageController() as! BookPageViewController<DataSource>
}
private func readingPageController() -> ReadingBookPageViewController {
return ReadingBookPageViewController(dataSource: ReadingBookPageDataSource())
}
private func starsPageController() -> StarsBookPageViewController {
return StarsBookPageViewController(dataSource: StarsBookPageDataSource())
}
}
The method currentPageController always crashes, because the DataSource is always equals to BookPageDataSource, not to ReadingBookPageDataSource or StarsBookPageDataSource.
Conceptual Discussion
Your concept for the architecture is flawed and this is leading to your issue.
Simple Generics Example
Here's a very simple example of a generic function, which just returns the value you give it:
func echo <T> (_ value: T) -> T { return value }
Because this function is generic, there is ambiguity about the type that it uses. What is T? Swift is a type-safe language, which means that ultimately there is not allowed to be any ambiguity about type whatsoever. So why is this echo function allowed? The answer is that when I actually use this function somewhere, the ambiguity about the type will be removed. For example:
let myValue = echo(7) // myValue is now of type Int and has the value 7
In the act of using this generic function I have removed the ambiguity by passing it an Int, and therefore the compiler has no uncertainty about the types involved.
Your Function
func currentPageController <DataSource> (at index: Int) -> BookPageViewController<DataSource>
Your function only uses the generic parameter DataSource in the return type, not in the input - how is the compiler supposed figure out what DataSource is?* I assume this is how you imagined using your function:
let pager = Pager()
let controller = pager.currentPageController(at: 0)
But now, what is the type of controller? What can you expect to be able to do with it? It seems that you're hoping that controller will take on the correct type based on the value that you pass in (0), but this is not how it works. The generic parameter is determined based on the type of the input, not the value of the input. You're hoping that passing in 0 will yield one return type, while 1 will yield a different one - but this is forbidden in Swift. Both 0 and 1 are of type Int, and the type is all that can matter.
As is usually the case with Swift, it is not the language/compiler that is preventing you from doing something. It is that you haven't yet logically formulated what is even is that you want, and the compiler is just informing you of the fact that what you've written so far doesn't make sense.
Solutions
Let's move on to giving you a solution though.
UIViewController Functionality
Presumably there is something that you wanted to use controller for. What is it that you actually need? If you just want to push it onto a navigation controller then you don't need it to be a BookPageViewController. You only need it to be a UIViewController to use that functionality, so your function can become this:
func currentPageController (at index: Int) -> UIViewController {
if index == 0 {
return readingPageController()
}
return starsPageController()
}
And you can push the controller that it returns onto a navigation stack.
Custom Functionality (Non-Generic)
If, however, you need to use some functionality which is specific to a BookPageViewController then it depends what it is you want to do. If there is a method on BookPageViewController like this:
func doSomething (input: Int) -> String
which doesn't make use of the generic parameter DataSource then probably you'll want to separate out that function into its own protocol/superclass which isn't generic. For example:
protocol DoesSomething {
func doSomething (input: Int) -> String
}
and then have BookPageViewController conform to it:
extension BookPageViewController: DoesSomething {
func doSomething (input: Int) -> String {
return "put your implementation here"
}
}
Now the return type of your function can be this non-generic protocol:
func currentPageController (at index: Int) -> DoesSomething {
if index == 0 {
return readingPageController()
}
return starsPageController()
}
and you can use it like this:
let pager = Pager()
let controller = pager.currentPageController(at: 0)
let retrievedValue = controller.doSomething(input: 7)
Of course, if the return type is no longer a UIViewController of any sort then you probably want to consider renaming the function and the related variables.
Custom Functionality (Generic)
The other option is that you can't separate out the functionality you need into a non-generic protocol/superclass because this functionality makes use of the generic parameter DataSource. A basic example is:
extension BookPageViewController {
func setDataSource (_ newValue: DataSource) {
self.dataSource = newValue
}
}
So in this case you really do need the return type of your function to be BookPageViewController<DataSource>. What do you do? Well, if what you really want is to use the setDataSource(_:) method defined above then you must have a DataSource object that you plan to pass in as an argument, right? If this is the case then we're making progress. Previously, you only had some Int value which you were passing into your function and the problem was that you couldn't specify your generic return type with that. But if you already have a BookPageDataSource value then it is at least logically possible for you to use this to specialize your
function.
What you say you want, however, is to just use an Int to get the controller at that index, regardless of what the DataSource type is. But if you don't care what the DataSource is of the returned BookPageViewController then how can you expect to set its DataSource to something else using the setDataSource(_:) method?
You see, the problem solves itself. The only reason you would need the return type of your function to be generic is if the subsequent functionality you need to make use of uses that generic type, but if this is the case then the controller you get back can't have just any old DataSource (you just wanted whichever one corresponds to the index you provide) - you need it to have exactly the type of DataSource which you plan to pass in when you use it, otherwise you're giving it the wrong type.
So the ultimate answer to your question is that, in the way that you were conceiving of it, there is no possible use for the function you were trying to construct. What's very cool about the way Swift is architected is that the compiler is actually able to figure out that logical flaw and prevent you from building your code until you've re-conceptualized it.
Footnote:
* It is possible to have a generic function which only uses the generic parameter in the return type and not in the input, but this won't help you here.
I want to store the value of a nested dictionary so my collection view can use it later without having to loop through stuff to find it every time. I also don't think it'll work (i could be wrong tho) if i try to put the loop in my cellforindexatpath function. The "let abilitiesArray: [Ability] =....." is inside my class
Ultimately I want abilitiesArray = abilities. "heroForDetails.hero.abilities" is a [String: [Ability]] so I want to return the [Ability] whose heroForDetails.hero.id == heroId because there are multiple key: value pairs in heroForDetails.hero.abilities. The current error is missing return in closure expected to return [Ability].
let abilitiesArray: [Ability] = { () -> ([Ability]) in
let val = heroForDetails.hero.abilities
for (heroId, abilities) in val {
if heroForDetails.hero.id == heroId {
return abilities
}
}
}()
"abilities" is an array that I plan to use like so
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("AbilitiesCell", forIndexPath: indexPath) as? AbilitiesCell {
let ability: Ability
ability = abilitiesArray[indexPath.row]
cell.configureCell(ability)
return cell
}else {
return UICollectionViewCell()
}
}
I hope i did a good job at explaining this. I just started learning to code so Any help would be great. I could be going about this completely wrong too.
In your first code snippet, you seem to be finding an item in the val dictionary using a key. I hope that's what you want to do. To do that, you don't need to loop through the whole dictionary. It's very slow to do so. (O(n)) You can use the subscript to access the dictionary using a key (the heroForDeails.hero.id variable). This is much faster (O(1)).
You can shorten the whole snippet into one line:
let abilitiesArray = heroForDetails.hero.abilities[heroForDetails.hero.id]!
Also, I don't see a question in your post...
You can access the dictionary by
let val = heroForDetails.hero.abilities
let abilities = val[heroForDeails.hero.id]
As far as accessing it in your cellforindexatpath, it should be just fine. Don't worry about the closure.
func callFunctionName(parameters: String) -> returnType
{
var somevalue = parameters
var returnValue = somevalue()
return returnValue
}
Is there a way to take in a input and use it as a function name?
example: let say input is green, I want to call function green. if input is red call function red etc...
Or to have a huge if statement to check each input to call different functions
This is not possible in Swift. You will have to store any functions you want to call in your own dictionary, and then use that to look up functions by name.
A "huge statement" might be feasible for a small number of functions, and it would certainly perform faster, but the ideal approach would be to store them in a dictionary.
However, if you are dealing with objects:
if exampleObject.respondsToSelector("exampleFunction")
{
exampleObject.performSelector("exampleFunction")
}
This approach currently works with all classes, be it Objective-C or Swift.
This is easily possible in Objective-C by using:
[self performSelector:NSSelectorFromString(#"green")];
But Swift is less dynamically typed than Objective-C and has less support for reflection. The Objective-C way I described above is very prone to crashes at runtime if the input (e.g. "purple" if you didn't have a function for purple) doesn't match a function that exists.
Using a big if statement is not an unreasonable way to approach it.
As the other answers said, an if statement is probably the best way to go about this.
override func viewDidLoad() {
if someValue = green {
green() //This will run whatever you have in the green() function below
}
}
func green() {
//put code for output of green here
}
Then all you have to do is make separate functions for all your outputs such as the green() func
This is the closest I could get. (And it's partially based on the answer of Vatsal Manot)
The idea is to use closures.
First of all we define the return type of the closure: let's use Int (of course you can change this later).
typealias colorClosureReturnType = Int
Now lets define the type of a closure that receives no parameters (you can change this too) and returns colorClosureReturnType
typealias colorClosureType = () -> (colorClosureReturnType)
Fine, now lets create a dictionary where the key is a String and the value is a closure of type colorClosureType:
let dict : [String: colorClosureType] = [
"red": { return 0 /* you can write here all the logic you need */ },
"green": { return 1 /* also here */},
"blue": { return 2 /* and here */}
]
Usually I let Swift Type Inference to infer the type of the variable/constant. But this time for sake of clarity I explicitly declared the type of the dictionary.
Now we can build a simple function that receives a String and return an optional colorClosureReturnType.
func callClosure(colorName: String) -> colorClosureReturnType? {
return dict[colorName]?()
}
As you can see the function look in the dictionary a closure associated to the key received as param. If it does found it then runs the closure and returns the results.
If the dictionary does not contain the requested key then the function returns nil. That's why the return type of this function is colorClosureReturnType? and not colorClosureReturnType.
Finally some tests:
callClosure("red") // 0
callClosure("green") // 1
callClosure("blue") // 2
func callFunctionName(parameters: String) -> ()
{
_ = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector(parameters), userInfo: nil, repeats: false)
}
func green() {
}
I'm developing a backend part of a system using Apple's Swift that needs to deal with email lists (an array of Email objects) and I need to set my own properties to this list such allowRepeated: Bool, and some methods like performCleanup() to remove duplicate entries if the allowRepeated property is set to true.
I have done something like that in a playground trying to follow the documentation notes about extending NSMutableArray class:
class EmailList: NSMutableArray {
override var count: Int {
get {
// It seems that the line below generates a infinity loop
return NSArray(array: self).count
}
}
override func insertObject(anObject: AnyObject, atIndex index: Int) {
insertObject(anObject, atIndex: index)
}
override func removeObjectAtIndex(index: Int) {
super.removeObjectAtIndex(index)
}
override func addObject(anObject: AnyObject) {
super.addObject(anObject)
}
override func removeLastObject() {
super.removeLastObject()
}
override func replaceObjectAtIndex(index: Int, withObject anObject: AnyObject) {
super.replaceObjectAtIndex(index, withObject: anObject)
}
override func objectAtIndex(index: Int) -> AnyObject {
return super.objectAtIndex(index)
}
}
It seems the line return NSArray(array: self).count is causing an infinite loop. Why is this happening?
Cocoa collection classes (such as NSMutableArray) are what's known as "class clusters" and are not meant to be subclassed. I guarantee any malfunctions are due to subclassing NSMutableArray.
In your case, I'm guessing when your strange count override tries to create an array with its own contents, it's once again asked for its count when the Array(array:) constructor is called, since Array(array:) presumably needs to know how long it'll be based on the passed source array. Lather, rinse, repeat.
The real question is why would you short-circuit a basic Cocoa class in this way? What possible gains do you feel you're getting by causing the creation of a whole new array when NSMutableArray.count works perfectly fine? Don't do things like this.
Indeed, NS(Mutable)Array isn't meant to be subclassed, and doing it in Swift is a recipe for disaster.
I recommend that you either write your own array class which uses a Swift Array as it's underlying type - a wrapper basically, or you just write external functions which take arrays as arguments. You could also do this with an NSMutableArray, but that would likely be unnecessary.
import SpriteKit
let NumOrientations: UInt32 = 4
enum Orientation: Int, Printable {
case Zero = 0, Ninety, OneEighty, TwoSeventy
var description: String {
switch self {
case .Zero:
return "0"
case .Ninety:
return "90"
case .OneEighty:
return "180"
case .TwoSeventy:
return "270"
}
}
static func random() -> Orientation {
return Orientation(rawValue: Int(arc4random_uniform(NumOrientations)))!
}
}
I am new to swift, but I have a lot of programming experience. However, I've never encountered anything like the variable "wrapping" when dealing with unknowns in Swift.
I have the static function random, which returns an Orientation. There is NOTHING optional about the Orientation class. However, I have to use an exclamation point on the return statement in the random function.
Why is this? Excuse my complete lack of knowledge about swift.
Well, obviously the initializer can fail. Let's assume:
Orientation(rawValue: 10)
This won't find a value in your enum, so what do you expect it to return? It will return nil. That means the return value must be an optional because nil can be returned.
This is explicitly mentioned in Swift Language Guide, Enumerations, Initializing from a Raw Value:
NOTE
The raw value initializer is a failable initializer, because not every raw value will return an enumeration member. For more information, see Failable Initializers.
However, in this case (method random) you are sure that a nil won't be returned, so the best solution is to unwrap the optional before returning it from random.