0

I am still trying to grasp all these programming concepts and I have a simple UIView with a rgb color and I am using this on many different views.

Right now I am still hard coding the color in every view:

UIColor(red: 120/255, green: 51/255, blue: 2/255, alpha: 1.0

But I'd love to create a single swift file (called Colors.swift for instance) and there I'd like to put all colors.

Now in every single view I'd like to just get the color from this file, so that I have only one place to change the colors if necessary.

I know how to create a function in a swift file and call it from another place. For example all animations I reuse I place into one file, but with those colors I'm having a blackout. I have no idea how to place them into another file and get them out of there. Perfect would be some kind of switch case with different named colors (blue, green,...) and their corresponding rgb values so that I can just call the name of the color to get it. This would be a swift file I create:

import UIKit

let myColors = UIColor()

switch myColors {
case .red:
    UIColor(red: 120/255, green: 51/255, blue: 2/255, alpha: 1.0)
case .gray:
    UIColor(red: 120/255, green: 120/255, blue: 120/255, alpha: 1.0)
default:
    UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)
}

But when I write a switch statement in a swift file I cannot get this to work. The error I get is "statements are not allowed on top level" and each case gets the error "Results of UIColor initializer is unused", but I do not see a way to place it inside something?

Maybe the whole approach is wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
RjC
  • 827
  • 2
  • 14
  • 33
  • 2
    Possible duplicate of [How can I make a Swift enum with UIColor value?](https://stackoverflow.com/questions/39112879/how-can-i-make-a-swift-enum-with-uicolor-value) – Umair M Dec 04 '17 at 11:46

3 Answers3

4

You can use Swift Extensions for this functionality.

// Colors.swift
extension UIColor {

   var myCustomRed: UIColor { 
      return UIColor(red: 120/255, green: 51/255, blue: 2/255, alpha: 1.0)
   }
}

// elsewhere in the app
view.backgroundColor = UIColor.myCustomRed

You can place as many custom implementations in there as you like, just be careful not to use conflicting names with existing colours.

For more information refer to the docs Swift Extensions Documentation

UPDATE:

enum ThemeColors: UIColor {
    case red = UIColor(red: 120/255, green: 51/255, blue: 2/255, alpha: 1.0)
    case blue = UIColor(red: 2/255, green: 51/255, blue: 120/255, alpha: 1.0) 
}

view.backgroundColor = ThemeColors.red
Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • Thanks. That works, but I wanted to put it into a class just to learn more of the concepts of functional programming, but the extension of UIColor seems to be mich easier in this case. – RjC Dec 04 '17 at 12:15
  • 1
    Using a class is considered Object orientated programming. Swift has extensions for exactly this type of thing. You could use a enum if you really wanted to, ill update with an example – Scriptable Dec 04 '17 at 12:16
  • Thanks so much for the help! – RjC Dec 04 '17 at 12:28
2

For Repeatedly Used Functions and Variables

Make one GlobalFunctions.swift class which have root class is NSObject and Declare your repeatedly used variables and Functions here

In Your case:-

import UIKit

class GlobalFunctions: NSObject {

    static let redColor = UIColor(red: 120/255, green: 51/255, blue: 2/255, alpha: 1.0
    static let greyColor = UIColor(red: 120/255, green: 120/255, blue: 120/255, alpha: 1.0)
    static let defaultColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)
}

You can use this color in your requiered View Controller like:

GlobalFunctions.redColor
GlobalFunctions.greyColor
GlobalFunctions.defaultColor

There is no need to create object for GloablFunctions.swift. You can directly access the color object because we have declared it as static variables. You can add class functions also to use recursively in your project.

Sachin Dobariya
  • 744
  • 7
  • 16
1

You need to put it into a class. You can simply make the color variable static, so you don't need to instantiate the class:

class Colors: NSObject {
    static let color: = UIColor(red: 120/255, green: 51/255, blue: 2/255, alpha: 1.0)
}

Then use it like this:

import Colors

Colors.color

You can also define your enums there.

Sune
  • 1,326
  • 1
  • 11
  • 17
  • Thanks, that was what I was looking for. This works well ex pet that when I try to create an enumeration I get the error "expected declaration". – RjC Dec 04 '17 at 12:13