I am trying to store random data into a second array - ios

I am trying to store randomly generated data into a second array so that I am able to press a button to go back to it. At the moment I press back and there is nothing there. I think it is due to the items not being correctly stored in the second array. I will provide the code for reference. Any help would be greatly appreciated!
var randomGeneratedQuotes = Array <String>()
var currentlyShown : String?
var randomQuote: String {
let randomNumber = Int(arc4random_uniform(UInt32(myQuotes.count)))
return myQuotes[randomNumber]
}
var previousRandom: String? {
if currentlyShown == randomQuote {
let previous = randomGeneratedQuotes.index(of: currentlyShown!)! - 1
return randomGeneratedQuotes[previous]
}
return randomGeneratedQuotes.last
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var randomQuote: String {
let randomNumber = Int(arc4random_uniform(UInt32(myQuotes.count)))
return myQuotes[randomNumber]
}
func storePrevious() {
if randomGeneratedQuotes.contains(randomQuote) {return}
randomGeneratedQuotes.append(randomQuote)
}
quotesLabel.text = randomQuote
}
//right hand button tapped
#IBAction func RHSButton(_ sender: Any) {
var randomQuote: String {
let randomNumber = Int(arc4random_uniform(UInt32(myQuotes.count)))
return myQuotes[randomNumber]
}
func storePrevious() {
if randomGeneratedQuotes.contains(randomQuote) {return}
randomGeneratedQuotes.append(randomQuote)
}
quotesLabel.text = randomQuote
}
//left hand button tapped
#IBAction func LHSButton(_ sender: Any) {
func pressedBack() {
currentlyShown = previousRandom
}
quotesLabel.text = previousRandom
}

Related

Not able to reload UICollectionView on button tap

I have one toggle button. Initially, it will be in off mode. So when I turn on my toggle I need to pass a set of data to collection view. And when its tun off again I need to pass a set of data to collection view.
So here is my code :
#IBAction func cateSelTap(_ sender: Any) {
isChecked = !isChecked
if isChecked {
self.subsetTheCategoryListForClientLogin(isCityLogin: false)
}else {
self.subsetTheCategoryListForClientLogin(isCityLogin: true)
}
}
func subsetTheCategoryListForClientLogin(isCityLogin: Bool) {
let cityType = "city"
print("LOG:CATE1 \(self.categoryarr)")
var objectIds = [Int]()
var subsetArray = NSMutableArray()
for i in 0..<self.categoryarr.count {
let type = (self.categoryarr.object(at:i) as AnyObject).value(forKey: "type") as! String
if isCityLogin {
if type == cityType {
subsetArray.add(self.categoryarr.object(at:i) as AnyObject)
objectIds.append(i)
}
} else {
if type != cityType {
subsetArray.add(self.categoryarr.object(at:i) as AnyObject)
objectIds.append(i)
}
}
}
self.categoryarr.removeAllObjects()
self.categoryarr = subsetArray
print("LOG:CATE2 \(self.categoryarr)")
DispatchQueue.main.async{
self.categorycollection.reloadData()
// self.categorycollection.performSelector(onMainThread: #selector(self.categorycollection.reloadData), with: nil, waitUntilDone: true)
}
}
When I run my app, and when I turn off - my collection view data is not showing exact data..still it is showing previous data. Again when I turn off its fully blank.
Any help on this?
Add a new property like categoryarr, say categoryCollectionViewSource and assign initial data to it. Use categoryCollectionViewSource in collection view delegate methods instead of categoryarr. And in the filter method
func subsetTheCategoryListForClientLogin(isCityLogin: Bool) {
let cityType = "city"
print("LOG:CATE1 \(self.categoryarr)")
var objectIds = [Int]()
var subsetArray = NSMutableArray()
for i in 0..<self.categoryarr.count {
let type = (self.categoryarr.object(at:i) as AnyObject).value(forKey: "type") as! String
if isCityLogin {
if type == cityType {
subsetArray.add(self.categoryarr.object(at:i) as AnyObject)
objectIds.append(i)
}
} else {
if type != cityType {
subsetArray.add(self.categoryarr.object(at:i) as AnyObject)
objectIds.append(i)
}
}
}
//note this line
self.categoryCollectionViewSource.removeAllObjects()
self.categoryCollectionViewSource = subsetArray
print("LOG:CATE2 \(self.categoryarr)")
DispatchQueue.main.async{
self.categorycollection.reloadData()
}
}
#IBAction func cateSelTap(_ sender: Any) {
isChecked = !isChecked
if isChecked {
self.subsetTheCategoryListForClientLogin(isCityLogin: false)
} else {
self.subsetTheCategoryListForClientLogin(isCityLogin: true)
}
}

Error when try to unwrap realm object

I have following issue when I try to use data from one of Realm Objects
I have an error:
Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
I'm confused because when I try to do it manually on debug console all works fine
(lldb) print meal?.mealName
(String?) $R0 = "Krem buraczany"
I will attach whole code and show you on which line error appear
Realm Object:
import UIKit
import RealmSwift
class Meal: Object {
#objc dynamic var mealID : String = ""
#objc dynamic var mealName : String = ""
var ingredients = List<String>()
var mealDescription = List<String>()
#objc dynamic var kcal : Int = 0
#objc dynamic var preparingTime : Int = 0
#objc dynamic var imageName : String = ""
convenience init(mealID: String, mealName: String, kcal: Int, preparingTime: Int, imageName: String) {
self.init()
self.mealID = mealID
self.mealName = mealName
self.kcal = kcal
self.preparingTime = preparingTime
self.imageName = imageName
}
override static func primaryKey() -> String? {
return "mealID"
}
}
Sending data via segue:
#IBAction func ideaForMealButtonPressed(_ sender: UIButton) {
performSegue(withIdentifier: "ideaForMeal", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! IdeaForMeal
destinationVC.randomMeal = getRandomMeal()
}
func getRandomMeal() -> String {
var mealsCount = realm.objects(Meal.self).count
mealsCount += 1
let randomMealID = arc4random_uniform(UInt32(mealsCount))
print (randomMealID)
return String(randomMealID)
}
Receiver:
//Global Variables
let realm = try! Realm()
var randomMeal : String? {
didSet {
let selectedMeal = downloadMealData(mealID: randomMeal!)
setUIForMeal(meal: selectedMeal)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
//Background Operations
func downloadMealData(mealID: String?) -> Meal? {
let currentMeal = realm.object(ofType: Meal.self, forPrimaryKey: mealID)
return currentMeal
}
func checkPositionOfTopIngredientsLabel() -> CGFloat {
let maxY = ingredientsTopLabel.frame.maxY
return maxY
}
func setUIForMeal(meal: Meal?) {
mealNameLabel.text = meal!.mealName // Error!
mealImage.image = UIImage(named: meal!.imageName)
preparingTimeLabel.text = String(meal!.preparingTime)
kcalLabel.text = String(meal!.kcal)
}
Thanks!
BR
iMat
The error is not related to Realm. It's a very common view life cycle mistake.
Setting randomMeal in the destination view controller triggers the didSet observer. The observer is going to access an IBOutlet which is not connected yet.
A solution is to run the code in viewDidLoad
var randomMeal = ""
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
if !randomMeal.isEmpty {
let selectedMeal = downloadMealData(mealID: randomMeal)
setUIForMeal(meal: selectedMeal)
}
}

Swift 4 - Quiz App Random Questions

I'm a beginner to Swift and generally Xcode. Thank you in advance for your time and help :)
I am doing a Quiz App, and I try to have random questions... I have a function to generate random numbers that I tried to apply in viewDidLoad()... unfortunately I can not guess how to use that information in his variable "var currentQuestion = 0"
This is the code:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
generateRandomNumber(0, 9, 10)
}
override func viewDidAppear(_ animated: Bool) {
newQuestion()
}
// Quiz//
let questions = ["Q1","Q2","Q3","Q4","Q5"]
let answers = [["A","2","3"],["B","2","3"],["C","2","3"],["D","2","3"],["E","2","3"]]
//Variables
var currentQuestion = 0
var rightAnswerPlacement:UInt32 = 0
//Generate Random Numbers
func generateRandomNumber(_ from:Int, _ to:Int, _ qut:Int?) -> [Int] {
var myRandomNumbers = [Int]() //All our generated numbers
var numberOfNumbers = qut //How many numbers to generate
let lower = UInt32(from) //Generate from this number..
let higher = UInt32(to+1) //To this one
if numberOfNumbers == nil || numberOfNumbers! > (to-from) + 1 {
numberOfNumbers = (to-from) + 1
}
while myRandomNumbers.count != numberOfNumbers {
let myNumber = arc4random_uniform(higher - lower) + lower
if !myRandomNumbers.contains(Int(myNumber)) {
myRandomNumbers.append(Int(myNumber))
}
}
return myRandomNumbers
}
//Label
#IBOutlet weak var lbl: UILabel!
//Button
#IBAction func action(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerPlacement)) {
print ("RIGHT!")
} else {
print ("WRONG!!!!!!")
}
if (currentQuestion != questions.count) {
newQuestion()
} else {
}
}
//Function that displays new question
func newQuestion() {
lbl.text = questions[currentQuestion]
rightAnswerPlacement = arc4random_uniform(3)+1
//Create a button
var button:UIButton = UIButton()
var x = 1
for i in 1...3 {
//Create a button
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement)) {
button.setTitle(answers[currentQuestion][0], for: .normal)
} else {
button.setTitle(answers[currentQuestion][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}
Any idea that how will be possible to resolve?
First you need to store the array of random questions that you generate (side note you appear to try to generate 10 random numbers but have only 5 questions).
Then instead of using the currentQuestion directly you use that to access the question in the array.
So change it to this:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
questionArray = generateRandomNumber(0, 9, 10)
}
override func viewDidAppear(_ animated: Bool) {
newQuestion()
}
// Quiz//
let questions = ["Q1","Q2","Q3","Q4","Q5"]
let answers = [["A","2","3"],["B","2","3"],["C","2","3"],["D","2","3"],["E","2","3"]]
//Variables
var currentQuestion = 0
var rightAnswerPlacement:UInt32 = 0
var questionArray: [Int] = [] // Just an initial value
//Generate Random Numbers
func generateRandomNumber(_ from:Int, _ to:Int, _ qut:Int?) -> [Int] {
var myRandomNumbers = [Int]() //All our generated numbers
var numberOfNumbers = qut //How many numbers to generate
let lower = UInt32(from) //Generate from this number..
let higher = UInt32(to+1) //To this one
if numberOfNumbers == nil || numberOfNumbers! > (to-from) + 1 {
numberOfNumbers = (to-from) + 1
}
while myRandomNumbers.count != numberOfNumbers {
let myNumber = arc4random_uniform(higher - lower) + lower
if !myRandomNumbers.contains(Int(myNumber)) {
myRandomNumbers.append(Int(myNumber))
}
}
return myRandomNumbers
}
//Label
#IBOutlet weak var lbl: UILabel!
//Button
#IBAction func action(_ sender: AnyObject) {
if (sender.tag == Int(rightAnswerPlacement)) {
print ("RIGHT!")
} else {
print ("WRONG!!!!!!")
}
if (currentQuestion != questions.count) {
newQuestion()
} else {
}
}
//Function that displays new question
func newQuestion() {
lbl.text = questions[questionArray[currentQuestion]]
rightAnswerPlacement = arc4random_uniform(3)+1
//Create a button
var button:UIButton = UIButton()
var x = 1
for i in 1...3 {
//Create a button
button = view.viewWithTag(i) as! UIButton
if (i == Int(rightAnswerPlacement)) {
button.setTitle(answers[questionArray[currentQuestion]][0], for: .normal)
} else {
button.setTitle(answers[questionArray[currentQuestion]][x], for: .normal)
x = 2
}
}
currentQuestion += 1
}
(I think the code is correct but I'm currently running some tests in Xcode so can't check. If there is a problem leave a comment.)

How to index an int array via button?

My code appends ints to an array. There is no sorting. I would like to index every entry to the array. Sort the array from smallest to largest.
import UIKit
class ViewController: UIViewController {
#IBOutlet var enterText: UITextField!
var arrayOfInt = [Int]()
#IBAction func submitText(_ sender: Any) {
if let text = enterText.text {
if let number = Int(text) {
arrayOfInt.append(number)
print(arrayOfInt)
} else {
print("Please enter number")
}
}
}
}
You can use like this :
#IBAction func submitText(_ sender: Any) {
if let text = enterText.text {
if let number = Int(text) {
var index = 0
for num in arrayOfInt {
if num > number {
arrayOfInt.insert(number, at: index)
break
}
index += 1
}
print(arrayOfInt)
} else {
print("Please enter number")
}
}
}

pass data with Button in swift

I try to pass data from my ViewController to TableViewPlace.swiftUse the buttons. When I am choosing button return not some thing
viViewController
class ViewController: UIViewController {
struct Country{
var id :Int
init(id:Int){
self.id = id
}
}
var CountrySelected = [Country]()
var countryArry = NSArray()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func airAction(sender: AnyObject) {
}
#IBAction func viewPlaceAction(sender: AnyObject) {
getParsePlaceView()
// json viewPlace
performSegueWithIdentifier("viewPlaceSegu", sender: sender)
}
#IBAction func tourAction(sender: AnyObject) {
}
/// Open the page
// parse json
func getParsePlaceView(){
let url = NSURL(string: "http://jsonplaceholder.typicode.com/posts")
NSURLSession.sharedSession().dataTaskWithURL(url!){ [unowned self] (data , repsonse , error) in
if error != nil {
print(error!)
} else {
do {
let posts = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers) as! [[String:AnyObject]]
for post in posts {
if let id = post["userId"] as? Int{
// print(id)
let sets = Country(id: id)
self.CountrySelected.append(sets)
}
}
self.countryArry = posts
print(self.countryArry)// return data and currect
} catch let error as NSError {
print(error)
}
}
}.resume()
print(countryArry) // return nil why??
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// get a reference to the second view controller
if segue.identifier == "viewPlaceSegu" {
if let secondViewController = segue.destinationViewController as? TableViewPlace {
// set a variable in the second view controller with the String to pass
print(countryArry)
secondViewController.tnt = countryArry
}
}
}
}
when I print(countryArry) return nil why ??
Can someone help me or give me a better solution?
You are creating a Post object with this line :
let countryPlace = Post(userid: post["userId"] as! Int, title: post["title"] as! String)
And you are trying to pass to your tableview this Post object to your tnt variable which is a ViewController class, this is why you've got the first error :
secondViewController.tnt = Country
Then, if you want to add your string country to your mutable array you have to do :
if let title = tnt.title {
Country.append(title)
}

Resources