0

Is it possible to convert a NSObject to an Int? and how can I implement this? I've look the Apple Reference Guide but I can't seem to understand how to use intValue under NSObject reference.

var intValue: Int32 { get }

I receive a NSObject from another class that is the HighestScore saved. I want to compare this HighestScore with the current Score and if current score is larger than highestScore send the current score to be saved. I have all that already except I'm not able to compare the NSObject (highest score) with my Int (current score)

Here is how I retrieve the HighestScore:

 func RetriveHighScore() -> NSObject {
    var dataToRetrive = HighScore()
    documentDirectories = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
    documentDirectory = documentDirectories.objectAtIndex(0) as String
    path = documentDirectory.stringByAppendingPathComponent("highScore.archive")
    if let dataToRetrive2 = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? HighScore {
        dataToRetrive = dataToRetrive2
    }

    return(dataToRetrive)
}

And here is how in another class I am calling the SaveHighScore class and the RetrieveHighScore function:

var bestScore = SaveHighScore.RetriveHighScore

How can I compare and Int value to my bestScore variable that is an NSObject

Thanks for your help

Wain
  • 118,658
  • 15
  • 128
  • 151
Raul Gonzalez
  • 875
  • 1
  • 10
  • 23
  • It isn't clear what you are actually trying to achieve. Maybe if you give some detail of the problem you are trying to solve by converting an NSObject to an Int it will be clearer. – Ben Trengrove Sep 23 '14 at 05:58
  • I just added more information, hope you can guide me on what to do – Raul Gonzalez Sep 23 '14 at 06:07
  • 1
    Does retrieveHighScore *has* to return an NSObject? – DCMaxxx Sep 23 '14 at 06:14
  • Yes, because the HighScore class that is also called in this function is a NSObject if I change the return type to Int, compiler marks an error in the return statement "HighScore is not a subtype of NSNumber". To return a Int i would need to convert dataToRetrive to Int. which is the same problem, how to convert and NSObject to Int – Raul Gonzalez Sep 23 '14 at 06:20
  • 1
    Why is your method not returning NSNumber then? – Tom Erik Støwer Sep 23 '14 at 06:22
  • 1
    You shouldn't be using `NSObject` here, you should be using `Int` or `NSNumber`. Change your high score class to return a sensible type. How are you creating the archive? Let the compiler know the true class type. – Wain Sep 23 '14 at 06:30
  • An NSObject is a dumb lump that contains nothing, does nothing. It is merely the base class for "real" classes such as NSNumber. – Hot Licks Oct 30 '14 at 17:05

1 Answers1

1

If an object is a 'real' AnyObject, but one knows that it represents a sequence of digits representing an Int number, then the following code works for me (Xcode Version 6.1 (6A1052c)):

 let oInt = (object as String).toInt() // in my app object has type _TtSQ , 
 var intResult = 0
 if oInt != nil { intResult = oInt! }  // intResult has type _TtSi

I tried to make it simpler but the snippet above is the only one that works

logc
  • 3,813
  • 1
  • 18
  • 29
tomekkreg
  • 11
  • 1