Custom class in Swift conversion - swift

I have created a custom class called PhoneTranslator (based on Xamarin's "Hello, iOS" guide). It looks like this:
class PhoneTranslator {
func ToNumber(raw:String) -> String {
var newNumber = raw.isEmpty ? "" : raw.uppercaseString
//newNumber operations...
return newNumber
}
}
Then I have a ViewController standard class. I want to do this:
var translatedNumber : String?
if let inputText = PhoneNumberTextField.text //getting text from UITextField
{
translatedNumber = PhoneTranslator.ToNumber(inputText) //error
}
Then in line with ToNumber method I get an error Cannot convert value of type 'String' to expected argument type 'PhoneTranslator'.
What am I doing wrong? All input and output types seems to match.

Change your code to
class PhoneTranslator {
class func ToNumber(raw:String) -> String {
var newNumber = raw.isEmpty ? "" : raw.uppercaseString
return newNumber
}
}
Your function was not a class function. Therefore you need an instance first. Above code defines the ToNumber function as class func.
Alternatively create an instance of the PhoneTranslator first:
translatedNumber = PhoneTranslator().ToNumber(inputText)
Note the () after PhoneTranslator.

Alternatively no need to make a class function. You can have a shared Instance which will be a singleton object for the entire application. With this singleton object you can call the PhoneTranslator methods
class PhoneTranslator {
static let sharedInstance = PhoneTranslator()
func ToNumber(raw:String) -> String {
var newNumber = raw.isEmpty ? "" : raw.uppercaseString
//newNumber operations...
return newNumber
}
}
And you can call it this way
translatedNumber = PhoneTranslator.sharedInstance.ToNumber(inputText)

Related

Pass a class type for use inside a method

In order to reduce cut-and-paste code in this app, I'm trying to pass class names around in order to tell a method which way it should process some data. I have something like the following:
class MyClass : NSObject {
var name : String = ""
}
class OneClass : MyClass {
override init() {
super.init()
self.name = "One"
}
}
class TwoClass : MyClass {
override init() {
super.init()
self.name = "Two"
}
}
class Thing : NSObject {
func doStuff(withClass cls: AnyClass) -> String {
let x = cls.init()
return x.name
}
}
let z = Thing()
print(z.doStuff(withClass: OneClass))
print(z.doStuff(withClass: TwoClass))
Passing withClass cls: AnyClass the parser pushed me to change let x = cls() to let x = cls.init(). But I've got an Expected member name or constructor call after type name error for the last two lines. The recommended fixes both cause other problems.
The first suggestion, adding the () constructor after the class name, causes new errors on those lines: Cannot convert value of type 'OneClass' to expected argument type 'AnyClass' (aka 'AnyObject.Type')
Taking the second suggestion and changing them to OneClass.self and TwoClass.self gets rid of the parser errors, but when I execute the code it just runs forever.. never erroring out, and never completing.
I found a recommendation elsewhere that suggests I should change the Thing.doStuff() parameters to expect MyClass instead of AnyClass, but that causes another set of new problems.
First, the parser starts complaining about the cls.init() call, and the series of fixes it suggests eventually lead to something that makes no sense: let x = cls.type(of:;; init)(). The parser ends up in a suggestion loop where it keeps adding more semi-colons in the middle of the statement.
Second, I'm back to type mismatch errors on the calls to doStuff() in the last two lines: Cannot convert value of type 'OneClass.Type' to expected argument type 'MyClass'.
There's obviously something I'm not getting here about passing types as arguments, but none of the googling I've done has landed me on something that explains the problems I'm seeing.
How about the generic Swift way.
The code constrains the generic type T to MyClass since it must have a name property.
class MyClass : NSObject {
var name : String
override required init() {
self.name = ""
super.init()
}
}
class OneClass : MyClass {
required init() {
super.init()
self.name = "One"
}
}
class TwoClass : MyClass {
required init() {
super.init()
self.name = "Two"
}
}
class Thing : NSObject {
func doStuff<T : MyClass>(withClass cls: T.Type) -> String {
let x = cls.init()
return x.name
}
}
let z = Thing()
print(z.doStuff(withClass: OneClass.self))
print(z.doStuff(withClass: TwoClass.self))
Or use a protocol.
protocol Nameable {
var name : String { get }
init()
}
class MyClass : NSObject, Nameable { ...
...
class Thing : NSObject {
func doStuff<T : Nameable>(withClass cls: T.Type) -> String {
let x = cls.init()
return x.name
}
}
To get this working, you must call init on cls after typecasting it to NSObject.Type. Also, x.name only works if cls Class type contains that particular property. This is the reason x is then typecasted to MyClass.
class Thing : NSObject
{
func doStuff(withClass cls: AnyClass) -> String?
{
let x = (cls as? NSObject.Type)?.init()
if let x = x as? MyClass
{
return x.name
}
return nil
}
}
Call doStuff with ClassType.self
print(z.doStuff(withClass: OneClass.self))
print(z.doStuff(withClass: TwoClass.self))
Let me know if you still face any issues.

Get object type from optional?

Is it possible to get the object type from an optional?
For example, if I have a class that has a property that is an optional string, can I somehow just get back the string type?
The exact use case I have is I have many custom classes all of which have a property that is storing another custom class as an optional value. I would like to write a generic function that will create an instance of the object class stored in the optional.
Here is an example of what I am looking for, although .dynamicType does not work since it is an optional:
class Class1 {
}
class Class2 {
var myOp: Class1?
}
var c = Class2()
c.myOp = c.myOp.dynamicType()
Since you wanted to use this with Generics I tried it for you. It works, but it may not be so useful.
First some setup:
This is a helper protocol to make sure our Generic type will have a known init method.
protocol ZeroParameterInit {
init()
}
This is an extension to get the type from an optional:
extension Optional {
var dynamicWrappedType : Wrapped.Type {
return Wrapped.self
}
}
Implemented in your code:
class Class1 : ZeroParameterInit {
required init() {}
}
class Class2 {
var myOp: Class1?
}
var c = Class2()
c.myOp = c.myOp.dynamicWrappedType.init()
Generic implementation:
class Class1 : ZeroParameterInit {
required init() {}
}
class Class2<T where T : ZeroParameterInit> {
var attribute: Optional<T>// used long syntax to remind you of : Optional<Wrapped>
init(attr:T) {
attribute = attr
attribute = nil
}
}
The function to create the instance:
func myFunc<T>(instance: Class2<T>) -> T {
return instance.attribute.dynamicWrappedType.init()
}
Some tests:
let alpha = Class1()
let beta = Class2(attr: alpha)
beta.attribute = myFunc(beta)
The issue:
You can't create an instance of Class2 without informing it about the type of it's generic attribute. So you need to pass it some object/type and that complicates things again.
Some extra methods that might improve how it all works:
init() {
}
let delta = Class2<Class1>()
delta.attribute = myFunc(delta)
init(type:T.Type) {
}
let epsilon = Class2(type: Class1.self)
epsilon.attribute = myFunc(epsilon)
You just need to check if the optional exist:
func myFunc(c: Class2) -> Class1? {
if let c1 = c.myOp{
return c1.dynamicType()
}
return nil
}
OR
func myFunc(c: Class2) -> Class1? {
if c.myOp != nil{
return c.myOp!.dynamicType()
}
return nil
}
Note the your return type need to be optional as well.
Tried this in simulator, seems like doing the right thing, if I understood you
class Class1 {
}
class Class2 {
var myOp: Class1?
}
func myFunc(c: Class2) -> AnyObject {
if let c1 = c.myOp{
return c1.self
}
return c
}
var object = Class2()
object.myOp = Class1()
myFunc(object) // Class1

Can I initialize a class with a variable which is defined by a function inside that class in Swift 2?

Is it possible that the variable which initializes my class is computed by a function inside that class?
I first initialize MyClass:
let MyClass = MyClass()
Second, this would be the code in the class:
class MyClass {
var myString = computeRandomString()
func computeRandomString() -> String {
piece of code
return(randomString)
}
}
Whenever I create an instance of MyClass I want the myString to be a randomString. In order for that I made a function within the same class.
There are two options.
First, if computeRandomString does not rely on knowing about anything about specific instances of this class, it could be converted to a class function, or simply moved outside of the class entirely.
For example:
class MyClass {
var myString = MyClass.computeRandomString()
class func computeRandomString() -> String {
return "random string"
}
}
or
func computeRandomString() -> String {
return "random string"
}
class MyClass {
var myString = computeRandomString()
}
The second option is to set it in the initializers (rather than giving it a default value), but you'll only be able to do this after all values (including this one) have been assigned a value.
For example:
class MyClass {
var myString: String
init() {
myString = ""
myString = computeRandomString()
}
func computeRandomString() -> String {
return "random string"
}
}
The reason we can't use an instance method to assign a default value for an instance variable is better explained by the warning generated when we try to use the instance method within an initializer before first giving it a value:
class MyClass {
var myString: String
init() {
myString = computeRandomString()
}
func computeRandomString() -> String {
return "random string"
}
}
On the myString =... line in init, we see the following error:
Use of 'self' in method call 'computeRandomString' before all stored properties are initialized.
This error, unfortunately, does not show up when we use it as the property's default value, as you're trying to do, but it does accurately describe the actual problem.
We cannot use self before our class is fully initialized, and that includes calling methods on self. And until all of our stored properties have valid values, our instance is not fully initialized, so we can never use an instance method to give a non-optional stored property its first value.
A possible solution is a lazy computed property.
The string is created when the property is accessed the first time
class MyClass {
lazy var computeRandomString : String = {
let alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789"
let alphaLength = UInt32(alphabet.characters.count)
var randomString : String = ""
for _ in 0..<20 {
let random = Int(arc4random_uniform(alphaLength))
let index = alphabet.startIndex.advancedBy(random)
randomString += String(alphabet[index])
}
return randomString
}()
}
for _ in 0..<10 {
print(MyClass().computeRandomString)
}
You have to do it like this:
class MyClass {
var myString: String
init() {
self.myString = MyClass.computeRandomString()
}
static func computeRandomString() -> String {
piece of code
return(randomString)
}
}
This will set myString to the results of computeRandomString() on initialisation (creating) of a MyClass-object.
I changed computeRandomString() to static because otherwise it could not be used before initialisation has finished, thanks to #nhgrif.

Swift Get value from function

I have two class and i would like to get a variable with the value (the variable is in a function) to my second class :
public class StreamPlayer {
class var sharedInstance : StreamPlayer{
struct Static {
static let instance : StreamPlayer = StreamPlayer()
}
return Static.instance
}
public var intermediate = NSString()
func metaDataUpdated(metaData : NSString){
var result : String = ""
var listItems = metaData.componentsSeparatedByString(";") as [String]
if (listItems.count > 0){
var containerName = listItems[0]
result = "StreamTitle=\'([^\"]*)\'".matchesForRegexIn(containerName, atRangeIndex: 1)
self.intermediate = result
}
}
}
and the second class
class RadioViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
println(test + StreamPlayer.sharedInstance.intermediate)
}
}
The problem is that the var intermediate doesn't change and don't get the value of result (in my first class)
I've copied your StreamPlayer class code into a playground. I've just commented out the matchesForRegexIn method as it seems it's your String extension so my code looks like this:
public class StreamPlayer {
class var sharedInstance : StreamPlayer{
struct Static {
static let instance : StreamPlayer = StreamPlayer()
}
return Static.instance
}
public var intermediate = String()
func metaDataUpdated(metaData : NSString){
var result : String = ""
let listItems = metaData.componentsSeparatedByString(";") as [String]
if (listItems.count > 0){
// var containerName = listItems[0]
result = "StreamTitle=\'([^\"]*)\'" //.matchesForRegexIn(containerName, atRangeIndex: 1)
intermediate = result
}
}
}
// calling the method to make sure intermediate gets updated
StreamPlayer.sharedInstance.metaDataUpdated("asd")
// check if it got updated
print(StreamPlayer.sharedInstance.intermediate)
The last line prints StreamTitle=\'([^\"])\'* so all is good. Just make sure to call StreamPlayer.sharedInstance.metaDataUpdated before checking intermediate
PS. I'm really not sure what you're trying to achieve by sharing intermediate results from a function to the outside world but it feels off. Think about splitting metaDataUpdated method into two methods maybe?
PPS. metaDataUpdated is a really bad name for a function
PPPS. If I were you I'd declare intermediate as String?
self.intermediate is a NSString while result is a String
Try
self.intermediate = result as NSString

Instance member cannot be used on type

I have the following class:
class ReportView: NSView {
var categoriesPerPage = [[Int]]()
var numPages: Int = { return categoriesPerPage.count }
}
Compilation fails with the message:
Instance member 'categoriesPerPage' cannot be used on type
'ReportView'
What does this mean?
Sometimes Xcode when overrides methods adds class func instead of just func. Then in static method you can't see instance properties. It is very easy to overlook it. That was my case.
You just have syntax error when saying = {return self.someValue}. The = isn't needed.
Use :
var numPages: Int {
get{
return categoriesPerPage.count
}
}
if you want get only you can write
var numPages: Int {
return categoriesPerPage.count
}
with the first way you can also add observers as set willSet & didSet
var numPages: Int {
get{
return categoriesPerPage.count
}
set(v){
self.categoriesPerPage = v
}
}
allowing to use = operator as a setter
myObject.numPages = 5
For anyone else who stumbles on this make sure you're not attempting to modify the class rather than the instance! (unless you've declared the variable as static)
eg.
MyClass.variable = 'Foo' // WRONG! - Instance member 'variable' cannot be used on type 'MyClass'
instanceOfMyClass.variable = 'Foo' // Right!
It is saying you have an instance variable (the var is only visible/accessible when you have an instance of that class) and you are trying to use it in the context of a static scope (class method).
You can make your instance variable a class variable by adding static/class attribute.
You instantiate an instance of your class and call the instance method on that variable.
Another example is, you have class like :
#obc class Album: NSObject {
let name:String
let singer:Singer
let artwork:URL
let playingSong:Song
// ...
class func getCurrentlyPlayingSongLyric(duration: Int = 0) -> String {
// ...
return playingSong.lyric
}
}
you will also get the same type of error like :
instance member x cannot be used on type x.
It's because you assign your method with "class" keyword (which makes your method a type method) and using like :
Album.getCurrentlyPlayingSongLyric(duration: 5)
but who set the playingSong variable before? Ok. You shouldn't use class keyword for that case :
// ...
func getCurrentlyPlayingSongLyric(duration: Int = 0) -> String {
// ...
return playingSong.lyric
}
// ...
Now you're free to go.
Your initial problem was:
class ReportView: NSView {
var categoriesPerPage = [[Int]]()
var numPages: Int = { return categoriesPerPage.count }
}
Instance member 'categoriesPerPage' cannot be used on type 'ReportView'
previous posts correctly point out, if you want a computed property, the = sign is errant.
Additional possibility for error:
If your intent was to "Setting a Default Property Value with a Closure or Function", you need only slightly change it as well. (Note: this example was obviously not intended to do that)
class ReportView: NSView {
var categoriesPerPage = [[Int]]()
var numPages: Int = { return categoriesPerPage.count }()
}
Instead of removing the =, we add () to denote a default initialization closure. (This can be useful when initializing UI code, to keep it all in one place.)
However, the exact same error occurs:
Instance member 'categoriesPerPage' cannot be used on type 'ReportView'
The problem is trying to initialize one property with the value of another. One solution is to make the initializer lazy. It will not be executed until the value is accessed.
class ReportView: NSView {
var categoriesPerPage = [[Int]]()
lazy var numPages: Int = { return categoriesPerPage.count }()
}
now the compiler is happy!
I kept getting the same error inspite of making the variable static.
Solution: Clean Build, Clean Derived Data, Restart Xcode. Or shortcut
Cmd + Shift+Alt+K
UserNotificationCenterWrapper.delegate = self
public static var delegate: UNUserNotificationCenterDelegate? {
get {
return UNUserNotificationCenter.current().delegate
}
set {
UNUserNotificationCenter.current().delegate = newValue
}
}
Just in case someone really needs a closure like that, it can be done in the following way:
var categoriesPerPage = [[Int]]()
var numPagesClosure: ()->Int {
return {
return self.categoriesPerPage.count
}
}