I am having trouble checking if the user in the JSON file has already been noticed by the user.
I have NSArray ( allEntries ) with Stored JSON in values. I create it like this:
let path = NSBundle.mainBundle().pathForResource("content", ofType: "json")
let jsonData : NSData = NSData(contentsOfFile: path!)!
allEntries = (try! NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)) as! NSArray
If I print one of the values allEntries [0] , I get:
{
answers = (
Chimney,
"Front door",
Window,
"Dog door"
);
difficulty = 1;
id = 2;
question = "How does Santa get into a house on Christmas eve?";
}
Now, later in my code, I need to call only one of the questions randomly, so I create an index and pass it to this function:
func LoadQuestion(index : Int)
{
let entry : NSDictionary = allEntries.objectAtIndex(index) as! NSDictionary
let questionID : Int = entry.objectForKey("id") as! Int
let question : NSString = entry.objectForKey("question") as! NSString
let arr : NSMutableArray = entry.objectForKey("answers") as! NSMutableArray
....
}
When the question is uploaded. I am adding this ID value to another array named already Asked
First I get this error:
Failed to pass value like "__NSCFString" (0x865ee0) to "NSNumber" (0x133781c).
in this line:
let questionID : Int = entry.objectForKey("id") as! Int
Int NSString:
let questionID : NSString = entry.objectForKey("id") as! NSString
:
let questionID = Int(questionIDRaw as! Int)
:
:
. Udemy . - , .
, , , , id ( json) hasaned. ?
:)
EDIT:
:
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var buttonA: UIButton!
@IBOutlet weak var buttonB: UIButton!
@IBOutlet weak var buttonC: UIButton!
@IBOutlet weak var buttonD: UIButton!
@IBOutlet weak var labelQuestion: UILabel!
@IBOutlet weak var labelScore: UILabel!
@IBOutlet weak var labelFeedback: UILabel!
@IBOutlet weak var buttonNext: UIButton!
@IBOutlet weak var BackgroundImageView: UIImageView!
var score :Int! = 0
var allEntries : NSArray!
var currentCorrectAnswerIndex : Int = 0
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
LoadAllQuestionsAndAnswers()
let randomNumber = Int(arc4random_uniform(UInt32(allEntries.count)))
LoadQuestion(randomNumber)
LoadScore()
AdjustInterface()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func LoadScore()
{
let defaults = NSUserDefaults.standardUserDefaults()
score = defaults.integerForKey("score")
labelScore.text = "score: \(score)"
}
func SaveScore()
{
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setInteger(score, forKey: "score")
}
func LoadAllQuestionsAndAnswers()
{
let path = NSBundle.mainBundle().pathForResource("content", ofType: "json")
let jsonData : NSData = NSData(contentsOfFile: path!)!
allEntries = (try! NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers)) as! NSArray
}
func LoadQuestion(index : Int)
{
let entry : NSDictionary = allEntries.objectAtIndex(index) as! NSDictionary
let question : NSString = entry.objectForKey("question") as! NSString
let arr : NSMutableArray = entry.objectForKey("answers") as! NSMutableArray
labelQuestion.text = question as String
let indices : [Int] = [0,1,2,3]
let newSequence = shuffle(indices)
var i : Int = 0
for(i = 0; i < newSequence.count; i++)
{
let index = newSequence[i]
if(index == 0)
{
currentCorrectAnswerIndex = i
}
let answer = arr.objectAtIndex(index) as! NSString
switch(i)
{
case 0:
buttonA.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 1:
buttonB.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 2:
buttonC.setTitle(answer as String, forState: UIControlState.Normal)
break;
case 3:
buttonD.setTitle(answer as String, forState: UIControlState.Normal)
break;
default:
break;
}
}
buttonNext.hidden = true
ResetAnswerButtons()
}
func shuffle<C: MutableCollectionType where C.Index == Int>(var list: C) -> C {
let total = list.count
for i in 0..<(total - 1) {
let j = Int(arc4random_uniform(UInt32(total - i))) + i
swap(&list[i], &list[j])
}
return list
}
func ResetAnswerButtons()
{
buttonA.alpha = 1.0
buttonB.alpha = 1.0
buttonC.alpha = 1.0
buttonD.alpha = 1.0
buttonA.enabled = true
buttonB.enabled = true
buttonC.enabled = true
buttonD.enabled = true
}
@IBAction func PressedButtonA(sender: UIButton) {
print("button A pressed")
buttonB.alpha = 0.3
buttonC.alpha = 0.3
buttonD.alpha = 0.3
buttonA.enabled = false
buttonB.enabled = false
buttonC.enabled = false
buttonD.enabled = false
CheckAnswer(0)
}
@IBAction func PressedButtonB(sender: UIButton) {
print("button B pressed")
buttonA.alpha = 0.3
buttonC.alpha = 0.3
buttonD.alpha = 0.3
buttonA.enabled = false
buttonB.enabled = false
buttonC.enabled = false
buttonD.enabled = false
CheckAnswer(1)
}
@IBAction func PressedButtonC(sender: UIButton) {
print("button C pressed")
buttonA.alpha = 0.3
buttonB.alpha = 0.3
buttonD.alpha = 0.3
buttonA.enabled = false
buttonB.enabled = false
buttonC.enabled = false
buttonD.enabled = false
CheckAnswer(2)
}
@IBAction func PressedButtonD(sender: UIButton) {
print("button D pressed")
buttonA.alpha = 0.3
buttonB.alpha = 0.3
buttonC.alpha = 0.3
buttonA.enabled = false
buttonB.enabled = false
buttonC.enabled = false
buttonD.enabled = false
CheckAnswer(3)
}
@IBAction func PressedButtonNext(sender: UIButton) {
print("button Next pressed")
let randomNumber = Int(arc4random_uniform(UInt32(allEntries.count)))
LoadQuestion(randomNumber)
PlaySoundButton()
}
func CheckAnswer( answerNumber : Int)
{
if(answerNumber == currentCorrectAnswerIndex)
{
labelFeedback.text = "Correct! +1"
labelFeedback.textColor = UIColor.greenColor()
score = score + 1
labelScore.text = "score: \(score)"
SaveScore()
PlaySoundCorrect()
}
else
{
labelFeedback.text = "Wrong answer"
labelFeedback.textColor = UIColor.redColor()
PlaySoundWrong()
}
buttonNext.enabled = true
buttonNext.hidden = false
}
func PlaySoundCorrect()
{
let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("correct", ofType: "mp3")!)
let error:NSError?
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
} catch let error1 as NSError {
error = error1
print(error)
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
func PlaySoundWrong()
{
let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("wrong", ofType: "wav")!)
var error:NSError?
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
} catch let error1 as NSError {
error = error1
print(error)
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
func PlaySoundButton()
{
let alertSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("button", ofType: "wav")!)
let error:NSError?
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: alertSound)
} catch let error1 as NSError {
error = error1
print(error)
}
audioPlayer.prepareToPlay()
audioPlayer.play()
}
func AdjustInterface()
{
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
buttonA.center = CGPointMake(screenWidth / 2, buttonA.center.y)
buttonB.center = CGPointMake(screenWidth / 2, buttonB.center.y)
buttonC.center = CGPointMake(screenWidth / 2, buttonC.center.y)
buttonD.center = CGPointMake(screenWidth / 2, buttonD.center.y)
buttonNext.center = CGPointMake(screenWidth / 2, buttonNext.center.y)
labelQuestion.center = CGPointMake(screenWidth / 2, labelQuestion.center.y)
BackgroundImageView.frame = CGRectMake(0, 0, screenWidth, screenHeight)
}
}
, , . , , , i: