JSA986
JSA986

Reputation: 5936

Trying to convert a simple OBJC fucntion to Swift

I have a simple function in OBJ C to create a random number

+(NSString*)certRef
{

NSInteger rNumber = arc4random() % 100000000 + 1;
NSString *randomCertRef = [NSString stringWithFormat: @"%ld", (long)rNumber];

return randomCertRef;
}

Now im trying to convert it Swift:

Class ToolsCustomFunctions

func randomNumber(theNumber: String){

    let rNumber = arc4random() % 100000000 + 1;
    let rNumberString = "\(rNumber)"
    println("THE RANDOM NUMBER IS: \(rNumberString)")

   return (theNumber)

}

The above gives Error "string not convertible to ()

Then im trying access it in class B

    let rNumberFuction = ToolsCustomFunctions()
    //read randomised number string here//

Various attempts including println "the random number is \(rNumberFuction.randomNumber(theNumber: String))" result in not accessing/setting the randomised number from my function either through compiler error or a nil value

Upvotes: 0

Views: 46

Answers (1)

Martin R
Martin R

Reputation: 540005

func randomNumber(theNumber: String) { ... }

is a function

  • taking one argument (a String), and
  • returning nothing (aka Void or ().

What you want is a function

  • without arguments, and
  • returning a string

which is declared as

func randomNumber() -> String { ... }

And the return value should be rNumberString, not theNumber.

Upvotes: 3

Related Questions