Utility Class with Array

Utility Class with array

As @Sulthan says, you really shouldn't use a utility class for this (much less a non-final utility class that inherits from NSObject!).

You should move hexStringToUIColor into a UIColor extension, which I would advise you also make a convenience initialiser. If you still want a namespace, you can use a caseless enum (this is preferred over a struct or class, as it prevents intialisation).

Also I would advise against using an array to store your hex color strings (unless you actually need to iterate over them for some reason). mColors[0] doesn't say "red", so make them static properties with actual names instead. They would also probably be a lot more useful as UIColor objects instead of Strings.

Here's an example of those suggestions:

extension UIColor {

convenience init(hex: String) {

var hex = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

if hex.hasPrefix("#") {
hex.remove(at: hex.startIndex)
}

guard hex.characters.count == 6 else {
self.init(cgColor: UIColor.gray.cgColor)
return
}

var rgbValue: UInt32 = 0
Scanner(string: hex).scanHexInt32(&rgbValue)

self.init(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255,
blue: CGFloat(rgbValue & 0x0000FF) / 255,
alpha: 1
)
}
}

enum MySpecialColors {
static let red = UIColor(hex: "#DA4167")
static let green = UIColor(hex: "#81E979")
static let blue = UIColor(hex: "#2B3A67")
static let yellow = UIColor(hex: "#FFFD82")
static let purple = UIColor(hex: "#3D315B")
}

Now if you want to use your colors, you just say things like:

mRingOne.fillColor = MySpecialColors.red

How to access a var array in a Utility class

This is a scope issue. You have declared the array choices inside the constructor of your Util class, and therefore it can only be referenced by name from inside this function. The first thing you will need to do is move it to the class level, like this.

public class Util{
//Also note you use [] for arrays, and {} for objects.
public var choices:Array = ["item 1", "item 2", "item 3"];
}

So, now that choices lives at the class level we can reference it by creating an instance of that class, like so...

//Inside another class somewhere...
var util:Util = new Util();
trace(util.choices[0]); // Outputs "item 1"

However, given your example it seems you only need one instance of this array for your entire program. An easy way to do this is by using the static modifier, which attaches the variable to the actual class (as opposed to an instance of the class) and you end up with something like this...

public class Util{
public static var choices:Array = ["item 1", "item 2", "item 3"];
}

//Anywhere else in your program
trace(Util.choices[0]); // Outputs "item 1"

Why are the 'Arrays' class' methods all static in Java?

In Java there is no way to extend the functionally of an array. Arrays all inherit from Object but this gives very little. IMHO This is a deficiency of Java.

Instead, to add functionality for arrays, static utility methods are added to classes like Array and Arrays. These methods are static as they are not instance methods.

PHP MVC: Injecting utility/helper class instances into base classes as dependencies

Something like Arrays::mergeArrays is not wrong. You don't mind call in_array or array_merge inside your classes, no? You can call Arrays an "utility" class if you want, but I don't think you can do the same for Files.

If your current problem is "I don't want pass the xyz object to each child controller", then usually you have schools of thought:

  • Just inject the DIC to your controllers. Then retrieve your dependency from it. That's what's called "Service Locator", and it's widely used by many frameworks. It has the advantage that you can require the dependency only where you need it, and when you need it. But it has a series of downside. Your object can't exists without it, and your classes can't be mocked easily. That's why it's considered an anti-pattern.

  • Inject your dependency (i.e: Files) explicitly in your controllers. Why you should want hide the fact that your controller depends on Files? The only problem here is, especially with controllers, that many dependencies aren't always used by all the class's methods. That's more a performance problem, and you can think of solve it in many ways (example:
    Action object).

Choose what works for you.

Utility methods for operating on custom scala class

Using your first example, to be able to invoke an operator concat as Array.concat(), there needs to exist an object called Array somewhere in scope that has concat as a method. The simplest way to do that would be:

object Array {
def concat[T](xs: Array[T], ys: Array[T]): Array[T] = ...
}

Otherwise you could define an abstract class (or trait) and have the object inherit it:

abstract class ArrayUtils {
// Implemented here
def concat[T](xs: Array[T], ys: Array[T]): Array[T] = ...
}

//Define the object
object Arrays extends ArrayUtils

// Or as a val
val Array = new ArrayUtils {}

Alternatively the abstract class could just define the operator and the object would implement it:

abstract class ArrayUtils {
// Defined here
def concat[T](xs: Array[T], ys: Array[T]): Array[T]
}

object Arrays extends ArrayUtils {
// Implemented here
def concat[T](xs: Array[T], ys: Array[T]): Array[T] = ...
}

The last is probably closest to what you wanted.

How to get type inference using a utility type, or type parameter, when I extend the built-in array class?

Simple fix: just add OmpaLoompa to the generic parameter of Array.

class WillyWonka extends Array<OmpaLoompa> { /* ... */ }

Otherwise TypeScript will assume that the elements of Array are of type any.



Related Topics



Leave a reply



Submit