I'm trying to make a 'Difficulty' type that can takes 3 states : easy, medium or hard. Then a 'minimum' and 'maximum' values will be set automatically and reachable like "myDifficultyInstance.min" or what.
I tried this but doesn't work, I get errors :
enum Difficulty {
case easy(min: 50, max: 200)
case medium(min: 200, max: 500)
case hard(min: 500, max: 1000)
}
Then I tried with a struct but it becomes too weird and ugly.
Is there a simple solution to do that ?
Default arguments are not allowed in enum cases
When you defining cases of enum, you can't define default values. Imagine it as you're just creating "patterns".
But what you can to is, that you can create default cases by creating static constants
enum Difficulty {
case easy(min: Int, max: Int)
case medium(min: Int, max: Int)
case hard(min: Int, max: Int)
static let defaultEasy = easy(min: 50, max: 200)
static let defaultMedium = medium(min: 200, max: 500)
static let defaultHard = hard(min: 500, max: 1000)
}
then you can use it like this
Difficulty.defaultEasy
Difficulty.defaultMedium
Difficulty.defaultHard
Also I think that for your case when you need to get min or max value, would be better if you were using custom data model
struct Difficulty {
var min: Int
var max: Int
static let easy = Difficulty(min: 50, max: 200)
static let medium = Difficulty(min: 200, max: 500)
static let hard = Difficulty(min: 500, max: 1000)
}
I know you accepted an answer already, but if you want to have both preset and customizable difficulty setting i'd suggest doing it like that:
enum Difficulty {
case easy
case medium
case hard
case custom(min: Int, max: Int)
var min : Int {
switch self {
case .easy:
return 50
case .medium:
return 200
case .hard:
return 500
case .custom(let min,_):
return min
}
}
var max : Int {
switch self {
case .easy:
return 200
case .medium:
return 500
case .hard:
return 1000
case .custom(_,let max):
return max
}
}
}
This way you're getting enumerated difficulties (finite exclusive states) with an option to define a custom one.
Usage:
let difficulty : Difficulty = .easy
let customDifficulty : Difficulty = .custom(min: 70, max: 240)
let easyMin = difficulty.min
let easyMax = difficulty.max
let customMin = customDifficulty.min
let customMax = customDifficulty.max
Related
I have a enum that has an integer representation, and I am going to be iterating over this type in a for loop. I was loop to get both the min and max int value from the enum
private enum PreloadedArrayDataIndex: Int, CaseIterable {
case Previous = 0
case Current = 1
case Future = 2
}
in this case, the min should return 0, for .Previous, and would return 2 for .Future.
I was looking to see if there is an easy 'swift' way to do this, something like
let minValue = PreloadedArrayDataIndex.allCases.min
let maxValue = PreloadedArrayDataIndex.allCases.max
I know that I could iterate over all the cases, and check each value against a stored max, but was looking to see if there was a different way I was not aware of.
The integers 0, 1, 2 are the “raw values” of the enumeration, and you can get the smallest and largest of the raw values with
let minValue = PreloadedArrayDataIndex.allCases.map(\.rawValue).min()! // 0
let maxValue = PreloadedArrayDataIndex.allCases.map(\.rawValue).max()! // 2
Another option is to make the enumeration comparable, so that you can determine the smallest and largest case:
enum PreloadedArrayDataIndex: Int, Comparable, CaseIterable {
case previous = 0
case current = 1
case future = 2
static func < (lhs: PreloadedArrayDataIndex, rhs: PreloadedArrayDataIndex) -> Bool {
lhs.rawValue < rhs.rawValue
}
}
let minCase = PreloadedArrayDataIndex.allCases.min()! // previous
let maxCase = PreloadedArrayDataIndex.allCases.max()! // future
I'd like to have a variable which can represent a single Measurement but may have different unit types. For example, it could store a length or a mass. It seems so simple, but I can't figure it out.
Here's what I tried:
struct Data {
var weight: Measurement<UnitMass>
var length: Measurement<UnitLength>
var target = "Weight"
var valueOfTarget: Measurement<Unit> {
if target == "Weight" {
return weight
} else {
return length
}
}
}
var data = Data(weight: Measurement<UnitMass>(value: 10, unit: UnitMass.kilograms),
length: Measurement<UnitLength>(value: 10, unit: UnitLength.centimeters),
target: "Weight")
print(data.valueOfTarget)
I also tried using <Dimension> as suggested in another answer, but that had a similar error.
This results in a compiler error:
error: cannot convert return expression of type 'Measurement<UnitMass>' to return type 'Measurement<Unit>'
Am I'm missing something obvious or is this just not possible?
You could just create new generic return values. This seems to compile OK for me.
struct Data {
var weight: Measurement<UnitMass>
var length: Measurement<UnitLength>
var target = "Weight"
var valueOfTarget: Measurement<Unit> {
if target == "Weight" {
return Measurement<Unit>(value: weight.value, unit: weight.unit)
} else {
return Measurement<Unit>(value: length.value, unit: length.unit)
}
}
}
var data = Data(weight: Measurement<UnitMass>(value: 10, unit: UnitMass.kilograms),
length: Measurement<UnitLength>(value: 10, unit: UnitLength.centimeters),
target: "Weight")
print(data.valueOfTarget)
First, don't make your own struct named Data, because Foundation already has a type named Data that's pretty widely used.
Second, since (in a comment) you said “It's for weight loss, the user can choose a target as either a weight or a waist size”, it seems like you should probably model this using an enum rather than a struct:
enum WeightLossTarget {
case weight(Measurement<UnitMass>)
case waistSize(Measurement<UnitLength>)
}
Third, if you really need to use a struct, you can fall back to the Objective-C type NSMeasurement for non-generic type:
struct WeightLossTarget {
enum TargetType {
case weight
case waistSize
}
var weight: Measurement<UnitMass>
var waistSize: Measurement<UnitLength>
var target: TargetType
var valueOfTarget: NSMeasurement {
switch target {
case .weight: return weight as NSMeasurement
case .waistSize: return waistSize as NSMeasurement
}
}
}
I need to display a fraction in my app but cannot find a good way to do it?
Should looks something like this
(proof of concept... don't need the font):
There is other posts similar to this but they are in ObJ C, I can not find a reliable solution in swift
This is just Apple sample code from the Introducing the New System Fonts video from WWDC 2015 put into a playground and using a UILabel to render plain text fractions using font features. [Updated for Swift 4]
//: Playground - noun: a place where people can play
import UIKit
import CoreGraphics
let pointSize : CGFloat = 60.0
let systemFontDesc = UIFont.systemFont(ofSize: pointSize,
weight: UIFont.Weight.light).fontDescriptor
let fractionFontDesc = systemFontDesc.addingAttributes(
[
UIFontDescriptor.AttributeName.featureSettings: [
[
UIFontDescriptor.FeatureKey.featureIdentifier: kFractionsType,
UIFontDescriptor.FeatureKey.typeIdentifier: kDiagonalFractionsSelector,
],
]
] )
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
label.font = UIFont(descriptor: fractionFontDesc, size:pointSize)
label.text = "12/48" // note just plain numbers and a regular slash
Just tap on the eye in the playground and you will see a beautiful fraction.
Introducing the New System Fonts (WWDC 2015 at 20:24)
I had to do something similar in an App. I created a mapping between common fractions and the related unicode characters like so:
enum Fraction: Double {
case Eighth = 0.125
case Quarter = 0.25
case Third = 0.333333333333333
case Half = 0.5
case TwoThirds = 0.666666666666667
case ThreeQuarters = 0.75
}
func localizedStringFromFraction(fraction: Fraction) -> String {
switch fraction {
case .Eighth:
return NSLocalizedString("\u{215B}", comment: "Fraction - 1/8")
case .Quarter:
return NSLocalizedString("\u{00BC}", comment: "Fraction - 1/4")
case .Third:
return NSLocalizedString("\u{2153}", comment: "Fraction - 1/3")
case .Half:
return NSLocalizedString("\u{00BD}", comment: "Fraction - 1/2")
case .TwoThirds:
return NSLocalizedString("\u{2154}", comment: "Fraction - 2/3")
case .ThreeQuarters:
return NSLocalizedString("\u{00BE}", comment: "Fraction - 3/4")
}
}
If you need support for more fractions, you can find the mapping here.
#KennethBruno's answer works but I find it a little inelegant. Here's my version:
let superscriptDigits = Array("⁰¹²³⁴⁵⁶⁷⁸⁹".characters)
let subscriptDigits = Array("₀₁₂₃₄₅₆₇₈₉".characters)
func vulgarFractionWithNumerator(numerator: UInt, denominator: UInt) -> String {
let zeroBias = UnicodeScalar("0").value
let supers = "\(numerator)".unicodeScalars.map { superscriptDigits[Int($0.value - zeroBias)] }
let subs = "\(denominator)".unicodeScalars.map { subscriptDigits[Int($0.value - zeroBias)] }
return String(supers + [ "⁄" ] + subs)
}
vulgarFractionWithNumerator(123, denominator: 45678)
Result:
"¹²³⁄₄₅₆₇₈"
Here is a Swift solution for arbitrary fractions:
enum ScriptType {
case Superscript
case Subscript
}
func createSuperOrSubscriptDigit(character:Character, type:ScriptType) -> Character {
switch character {
case "0": return type == .Superscript ? "\u{2070}" : "\u{2080}"
case "1": return type == .Superscript ? "\u{00b9}" : "\u{2081}"
case "2": return type == .Superscript ? "\u{00b2}" : "\u{2082}"
case "3": return type == .Superscript ? "\u{00b3}" : "\u{2083}"
case "4": return type == .Superscript ? "\u{2074}" : "\u{2084}"
case "5": return type == .Superscript ? "\u{2075}" : "\u{2085}"
case "6": return type == .Superscript ? "\u{2076}" : "\u{2086}"
case "7": return type == .Superscript ? "\u{2077}" : "\u{2087}"
case "8": return type == .Superscript ? "\u{2078}" : "\u{2088}"
case "9": return type == .Superscript ? "\u{2079}" : "\u{2089}"
default: return character
}
}
func createSuperOrSubscriptDigits(string:String, type:ScriptType) -> String {
return String(string.characters.map() { createSuperOrSubscriptDigit($0, type: type) })
}
extension String {
func createSuperscriptDigits() -> String {
return createSuperOrSubscriptDigits(self, type: .Superscript)
}
func createSubscriptDigits() -> String {
return createSuperOrSubscriptDigits(self, type: .Subscript)
}
}
func fractionString(numerator:String, denominator:String) -> String {
return numerator.createSuperscriptDigits() + "\u{2044}" + denominator.createSubscriptDigits()
}
So I have several enums representing various unit systems:
enum MassUnit:Double{
case Pound = 453.59237, Ounce = 28.349523125, Gram = 1.0, Kilogram = 1000.0;
}
enum VolumeUnit:Double{
case Teaspoon = 1, Tablespoon = 3, Cup = 48, Pint = 96, Quart = 192, Gallon = 768, Liter = 202.884136211, Milliliter = 0.202884136211
}
enum TimeUnit:Double{
case Second = 1, Minute = 60, Hour = 3600, Day = 86400, Week = 604800, Year = 31536000
}
What I would like to do is be able to convert from one unit to another, eg from year to seconds. To do this I've ensured that the raw values for my enums correspond to the converting multipliers, eg 1 Min = 60 seconds. Thus, given x amount of some unit, the conversion is simply
x * rawValue1 / rawValue2 // rawValue2 = rawValue of desired unit.
While that conversion is simple enough, I would love to be efficient and use a protocol:
protocol Convertable{
func convert(inputAmount inputAmount:Double, outputUnit:Self)->Double;
}
Then, I could extend the enum:
extension TimeUnit:Convertable{
func convert(inputAmount inputAmount: Double, outputUnit: TimeUnit) -> Double {
return inputAmount * self.rawValue / outputUnit.rawValue;
}
}
Then I could simply convert like this:
TimeUnit.Year.convert(inputAmount: 2.54, outputUnit: .Second)
// returns 80101440
This is great, however, depending on how many units I want to convert, there would be a lot of duplication of the same code.
So, what I would like to do is somehow use a protocol extension.
extension Convertable{
func convert(inputAmount inputAmount: Double, outputUnit: Self) -> Double {
return inputAmount * self.rawValue / outputUnit.rawValue;// Compile error...
}
}
This is where I get into trouble, the output unit is declared as self, which knows nothing about rawValue.
Any ideas?
Right when I was asking this question, the answer came to me: Have the Convertible protocol also require a rawValue variable:
protocol Convertable{
var rawValue:Double{get}
func convert(inputAmount inputAmount:Double, outputUnit:Self)->Double;
}
That way, referencing self.rawValue is known, and since enums already have a rawValue, no extra work is required:
enum MassUnit:Double, Convertable{
case Pound = 453.59237, Ounce = 28.349523125, Gram = 1.0, Kilogram = 1000.0;
}
enum VolumeUnit:Double, Convertable{
case Teaspoon = 1, Tablespoon = 3, Cup = 48, Pint = 96, Quart = 192, Gallon = 768, Liter = 202.884136211, Milliliter = 0.202884136211
}
enum TimeUnit:Double, Convertable{
case Second = 1, Minute = 60, Hour = 3600, Day = 86400, Week = 604800, Year = 31536000
}
And then using the converter:
TimeUnit.Year.convert(inputAmount: 2.54, outputUnit: .Second) // 80101440
MassUnit.Gram.convert(inputAmount: 20.0, outputUnit: .Ounce) //0.70547
VolumeUnit.Pint.convert(inputAmount: 0.2, outputUnit: .Tablespoon)// 6.4000
Even beter, in the odd case where a conversion doesn't work like this, I can override the convert function with my own implementation, such as in Temperature:
enum TemperatureUnit:Double, Convertable{
case Celsius, Fahrenheit
func convert(inputAmount inputAmount: Double, outputUnit: TemperatureUnit) -> Double {
if self == outputUnit {
return inputAmount;
} else if self == .Celsius {
return inputAmount * 9.0/5.0 + 32.0
} else {
return (inputAmount - 32.0) * 5.0 / 9.0;
}
}
}
TemperatureUnit.Celsius.convert(inputAmount: 3, outputUnit: .Fahrenheit) // 37.4
TemperatureUnit.Fahrenheit.convert(inputAmount: 0, outputUnit: .Celsius) // -17.7778
Beautiful!
I'm working with Swift, Xcode 6 and SpriteKit,
I want make a game where some sprites fall down from the top of the screen, but each sprite have a defined speed, position and activation time. I have this working code, but I really don't think that it's the most optimised way to do it:
class obstacles: SKSpriteNode
{
var isOnScreen = false
var type: Int = 0
var initTime: Int = 0
}
var obstacle = [obstacles]() // obstacle is an array of SKSpriteNode
// type : initTime : speed : position : xScale : yScale
var level1: [String] = ["0:120:3:0:1:1", "0:130:4:80:2:2","1:140:8:120:6:1","0:150:6:240:2:2"]
override func didMoveToView(view: SKView)
{
initObstacles()
}
func initObstacles()
{
for var x = 0; x < level1.count; x++ // for each obstacle
{
var index = 0
var type = String("")
var initTime = String("")
var speed = String("")
var position = String("")
var xScale = String("")
var yScale = String("")
var lastIndex = obstacle.count
for Character in level1[x] // we read each character one by one
{
if Character == ":" { index++ } // if it's a ":" we change the variable
else
{
switch index
{
case 0:
type += String(Character)
case 1:
initTime += String(Character)
case 2:
speed += String(Character)
case 3:
position += String(Character)
case 4:
xScale += String(Character)
case 5:
yScale += String(Character)
default:
break;
}
}
}
obstacle.append(obstacles(imageNamed: "Rectangle")) // we add an element to the array
obstacle[lastIndex].type = type.toInt()! // we init all the values
obstacle[lastIndex].initTime = initTime.toInt()!
obstacle[lastIndex].speed = CGFloat(speed.toInt()!)
obstacle[lastIndex].size.width = DEFAULT_OBSTACLE_SIZE * CGFloat(xScale.toInt()!)
obstacle[lastIndex].size.height = DEFAULT_OBSTACLE_SIZE * CGFloat(yScale.toInt()!)
obstacle[lastIndex].position = CGPointMake(CGFloat(position.toInt()!) - obstacle[lastIndex].size.width/2, CGRectGetMaxY(frame) + obstacle[lastIndex].size.height/2)
}
}
Do you know how could I manage to do the same thing, but more "clean" ?
I would suggest to create a class or struct that holds all necessary data for an obstacle and additionally change your type from a standard number to an enum, e.g.:
enum ObstacleType {
case Block, Tree, WhatEverObstaclesYouHave...
}
struct Obstacle {
var type: ObstacleType
var initializationTime: NSTimeInterval
var speed: Double
// and similarly for position, your scales and what you may need in future
}
and create them using, e.g.
Obstacle(type: .Block, initializationTime: 0, speed: 12.0, ...)
Advantage (obviously) is that you have no problems anymore parsing your string (there is no string anymore) and can provide all necessary information using the appropriate type directly. And you can easily use an enum for your type, which should be an enum, because your Obstacle is not a 1, but a Block, Tree or whatever.
Your level1 variable could then be defined like this:
var level1 : [Obstacle] = [
Obstacle(type: .Block, initializationTime: 120, speed: 3.0, ...),
Obstacle(type: .Block, initializationTime: 130, speed: 4.0, ...),
Obstacle(type: .Tree, initializationTime: 140, speed: 8.0, ...),
Obstacle(type: .Block, initializationTime: 150, speed: 6.0, ...)
]
To get rid of the labels in the initializer, just define your own initializer. Just add this to your struct:
init(_ type: ObstacleType, _ time: NSTimeInterval, _ speed: Double, ...) {
self.type = type
self.initializationTime = time
self.speed = speed
// other variables...
}
Then you can create every Obstacle like this:
Obstacle(.Block, 120, 3.0, ...)
But now you can not easily tell anymore which number has what meaning from reading the instantiation. I do not recommend this just to type less as autocomplete will present you with most of it.