How to select items from array no repeat - arrays

I'm building swift quiz app I want to show random questions with no repeat.
var sorular: Array = ["soru1","soru2","soru3","soru4","soru5"]
var gorulensoru = [Int]()
var sayac: Int = 0
var sorularcevaplar = ["D","Y","D","Y","D"]
var cevaplar: Array<Any> = []
var dogru: Int = 0
var yanlis: Int = 0
func chooseRandom() -> String {
if gorulensoru.count == sorular.count { return "" }
let randomItem = Int(arc4random() % UInt32(sorular.count)) //get
if (gorulensoru.contains(randomItem)) {
return chooseRandom()
}
let requiredItem = sorular[randomItem]
gorulensoru.append(randomItem)
return requiredItem
}
override func viewDidLoad() {
super.viewDidLoad()
soruText.text = chooseRandom()
}
What is the problem in my code? I'm tried insert selected random item to inside gorulensoru array but it shows again selected item
if (gorulensoru.contains(randomItem)) {
return chooseRandom()
}
This statement doesn't run.

Your code only runs once.
Also, you shouldn't include potential infinite recursive calls because it can easily get to a level where it causes a hang or crash.
Use shuffle() and then iterate over the array.

Change this and it may work:
let requiredItem = sorular[randomItem]
gorulensoru.append(requiredItem)

Related

convert array of string into Double in swift

I'm trying to convert a string into a double in swift. I managed to extract the string from a website (www.x-rates.com) into an array but I cannot convert it after in a double in order to make some work around this number. Can anyone tell me what I'm supposed to do or what I did wrong? I know that my label don't update now but I will do it later, the first thing that I'm trying to do is the conversion.
thx a lot!
Here is the code:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var moneyTextField: UITextField!
#IBAction func convert(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string: "https://www.x-rates.com/calculator/?from=EUR&to=USD&amount=1")!
let request = NSMutableURLRequest(url : url)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
var message = ""
if let error = error {
print(error)
} else {
if let unwrappedData = data {
let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)
var stringSeperator = "<span class=\"ccOutputRslt\">"
if let contentArray = dataString?.components(separatedBy: stringSeperator){
if contentArray.count > 0 {
stringSeperator = "<span"
let newContentArray = contentArray[1].components(separatedBy: stringSeperator)
if newContentArray.count > 0 {
message = newContentArray[0]
var message = Float(newContentArray[0])! + 10
}
}
}
}
}
DispatchQueue.main.sync(execute: {
self.resultLabel.text = "the value of the dollar is " + message
}
)}
task.resume()
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I will talk about convert an Array of String to Array of Double.
In swift Array has a method called map, this is responsable to map the value from array, example, in map function you will receive an object referent to your array, this will convert this object to your new array ex.
let arrOfStrings = ["0.3", "0.4", "0.6"];
let arrOfDoubles = arrOfStrings.map { (value) -> Double in
return Double(value)!
}
The result will be
UPDATE:
#LeoDabus comments an important tip, this example is considering an perfect datasource, but if you have a dynamic source you can put ? on return and it will work, but this will return an array with nil
like that
let arrOfStrings = ["0.3", "0.4", "0.6", "a"];
let arrOfDoubles = arrOfStrings.map { (value) -> Double? in
return Double(value)
}
Look this, the return array has a nil element
If you use the tips from #LeoDabus you will protect this case, but you need understand what do you need in your problem to choose the better option between map or compactMap
example with compactMap
let arrOfStrings = ["0.3", "0.4", "0.6", "a"];
let arrOfDoubles = arrOfStrings.compactMap { (value) -> Double? in
return Double(value)
}
look the result
UPDATE:
After talk with the author (#davidandersson) of issue, this solution with map ou contactMap isn't his problem, I did a modification in his code and work nice.
first I replaced var message = "" per var rateValue:Double = 0.0 and replacedFloattoDouble`
look the final code
let url = URL(string: "https://www.x-rates.com/calculator/?from=EUR&to=USD&amount=1")!
let request = NSMutableURLRequest(url : url)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
var rateValue:Double = 0.0;
if let error = error {
print(error)
} else {
if let unwrappedData = data {
let dataString = NSString(data: unwrappedData, encoding: String.Encoding.utf8.rawValue)
var stringSeperator = "<span class=\"ccOutputRslt\">"
if let contentArray = dataString?.components(separatedBy: stringSeperator){
if contentArray.count > 0 {
stringSeperator = "<span"
let newContentArray = contentArray[1].components(separatedBy: stringSeperator)
if newContentArray.count > 0 {
rateValue = Double(newContentArray[0])! + 10
}
}
}
}
}
//
print("Rate is \(rateValue)"); //Rate is 11.167
}
task.resume()
Hope to help you
The reason your code doesn’t work in my opinion is that you have two variables with the same name that are defined in different scopes and you use the wrong one at the end.
At the beginning you define
var message = ""
And then when converting to a number further down
var message = Float(newContentArray[0])! + 10
So change the last line to something like
var number = Float(newContentArray[0])! + 10
And use number in your calculations. Although I think
var number = Double(message)
should work equally fine since you have assigned newContentArray[0] to message already and Double is more commonly used than Float (I don’t understand + 10)

Issue with storing data within an Array - Swift

I currently have my set-up as followed:
I am running a query in Firebase to extract all of the genres within an array of genres, like so:
var genresLabelIndex : [String] = ["Horror", "Fiction", "Romance"]
Then I am creating a blank arrays for each of the genres to be able to store the information of the genres within each of the areas like so:
var horrorData = [InformationForFeed]()
var fictionData = [InformationForFeed]()
var romanceData = [InformationForFeed]()
InformationForFeed looks like so:
class InformationForFeed {
fileprivate var _uploadKey:String!
fileprivate var _userKey:String!
fileprivate var _imageURL:String!
fileprivate var _socialMedia:[String]
var uploadKey:String!{
return _uploadKey
}
var userKey:String!{
return _userKey
}
var imageURL:String!{
return _imageURL
}
init(dictionary:Dictionary<String,AnyObject>, socials: [String]) {
_socialMedia = socials
if let uploadKey = dictionary["upload_key"] as? String {
self._uploadKey = uploadKey
}
if let userKey = dictionary["user_key"] as? String {
self._userKey = userKey
}
if let imageURL = dictionary["imageUrl"] as? String {
self._imageURL = imageURL
}
}
}
I am then creating an Array of the list of genres arrays like so:
1) First I am creating an empty array of arrays like this:
var genreArrayIndex : [[InformationForFeed]] = []
2) Then within my init() of the UIView I am setting what will be in the array like this:
genreArrayIndex = [self.horrorData, self.fictionData, self.romanceData]
I then will run a function called getData() that will run my query and start storing the information.
I store my information of each genre in a tempArray, and then I set the genreArrayIndex[index] to equal the tempArray and then clear the tempArray as seen in getData below.
func getData() {
for genre in genresLabelIndex {
let dbReference = Database.database().reference().child("genres").child(genre)
let query = dbReference.queryLimited(toLast: 6)
query.observeSingleEvent(of: .value, with: { (snapshot : DataSnapshot) in
if snapshot.childrenCount > 0 {
for s in snapshot.children.allObjects as! [DataSnapshot] {
let item = s.value as! Dictionary<String,AnyObject?>
let facebook = (s.childSnapshot(forPath: "social_links").value as? NSDictionary)?["facebook_link"]
let audible = (s.childSnapshot(forPath: "social_links").value as? NSDictionary)?["audible_link"]
let amazon = (s.childSnapshot(forPath: "social_links").value as? NSDictionary)?["amazon_link"]
var socialsArray = [String]()
if facebook != nil {
socialsArray.append(facebook! as! String)
} else {
socialsArray.append("nil")
}
if audible != nil {
socialsArray.append(audible! as! String)
} else {
socialsArray.append("nil")
}
if amazon != nil {
socialsArray.append(amazon! as! String)
} else {
socialsArray.append("nil")
}
let data = InformationForFeed(dictionary: item as Dictionary<String,AnyObject>, socials: socialsArray)
self.newArray.append(data)
}
}
self.genreArrayIndex[self.genreArrayIndexCount] = self.newArray
self.genreArrayIndexCount = self.genreArrayIndexCount + 1
self.newArray.removeAll()
self.internalIndex = self.internalIndex + 1
if self.internalIndex == self.genresLabelIndex.count {
self.tableView.reloadData()
}
})
}
}
My tempArray looks like this:
var newArray = [InformationForFeed]()
The index looks like this:
var genreArrayIndexCount : Int = 0
Now comes the issue....
All of the information is properly being stored in the genreArrayIndex .....but... it is not actually storing the information in the arrays that being stored in genreArrayIndex.
So in other words if you were to print(self.genreArrayIndex) it would be fully populated. But if you were to print(self.fictionData) it would be blank.
How can I resolve this?
Array is a value type. That means its contents are copies. Initializing genreArrayIndex with empty horrorData, (and others) and then filling it with getData() does not also copy the data back into horrorData. I would recommend eliminating
genreArrayIndex = [self.horrorData, self.fictionData, self.romanceData]
and replacing horrorData, fictionData, ... with computed properties. Perhaps like this.
var horrorData: [InformationFeed] {
return genreArrayIndex[0]
}

How to store array values depending on a condition?

I have variables of type string, [int], bool
var books:[String] = ["Hobbit","LOTR","Fellowship"]
var chaptersToRead:[[Int]] = [[1],[1,3],[2,3]]
var read:[Bool] = [false,true,true]
I have function display() so that I can see details about all the books individually
func display() -> [[Any]] {
var output = [[Any]]()
for i in 0..<books.count {
output.append([books[i], chaptersToRead[i], read[i]])
}
return output
}
I like to store values of books in two arrays based on condition if chaptersToRead = 1, as follows
var booksAssigned:[String] = ["Hobbit","LOTR"]
var readStatus:[Bool] = [false,true]
I tried to get the above result by doing the following below which is not working. What am I doing wrong?
var booksAssigned:[String] = []
var readStatus:[Bool] = []
for (index, books) in display().enumerated()
{
if chaptersToRead.joined().contains(1)
{
//I am getting signal SIGABRT error here
booksAssigned = books as! [String]
}
}
for (index, status) in display().enumerated()
{
if chaptersToRead.joined().contains(1)
{
//I am getting signal SIGABRT error here
readStatus = status as! [Bool]
}
}
Try this:
var booksAssigned:[String] = []
var readStatus:[Bool] = []
for (index, chapters) in chaptersToRead.enumerated() {
if chapters.contains(1) {
booksAssigned.append(books[index])
readStatus.append(read[index])
}
}
print(booksAssigned)
print(readStatus)
Edit: Edited as per #Nirav D's suggestion.
Remove var keyword from if blocks. You have already declared those variables.
var booksAssigned:[Any] = []
var readStatus:[Any] = []
for (index, books) in display().enumerated()
{
if chaptersToRead.joined().contains(1)
{
booksAssigned = books
}
}
for (index, status) in display().enumerated()
{
if chaptersToRead.joined().contains(1)
{
readStatus = status
}
}
This works.

WKPickerItems and ranges

I am trying to make a picker view in Watch Kit from -273 to 273. Sadly Watch Kit only allows a string as title so I converted my range using .map Now when I run the App it display a range from 0 to 546, but will not go into the negative range I tried changing both values but the picker always starts at 0 and won't go back further.
I isolated the problem into these lines:
let pickerItems: [WKPickerItem] = (-273...273).map {
let pickerItem = WKPickerItem()
pickerItem.title = "\($0)"
return pickerItem
}
I just tried it out and your code does show negative values in the picker. Then when the user picks a value you have to take the value and use it to retrieve the "real" value from your pickerItems array:
class InterfaceController: WKInterfaceController {
#IBOutlet var picker: WKInterfacePicker!
var pickerItems: [WKPickerItem]?
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
pickerItems = (-273...273).map {
let pickerItem = WKPickerItem()
pickerItem.title = "\($0)"
return pickerItem
}
picker.setItems(pickerItems)
}
#IBAction func pickerDidChange(value: Int) {
let pickedItem = pickerItems![value] // value = 0..576
if let pickedValue = Int(pickedItem.title!) {
print(pickedValue) // -273..273
}
}
}

Best Practice using an Array of Strings with NSUserDefault

I'm having a bit trouble saving an array of strings to userDefaults. I have an Array of strings declaired in a class, with a property observer ment to sync to userDefaults. Furthermore, I want the array to be limited to a maximum of 5 Strings.
let userDefaults = NSUserDefaults.standardUserDefaults()
var suggestions: [String]! {
didSet {
var arrayEnd = suggestions.count
if arrayEnd >= 5 {
arrayEnd = 4
}
let newArray = Array(suggestions[0...arrayEnd])
userDefaults.setObject(newArray, forKey: "suggestions")
userDefaults.synchronize()
}
}
func getSuggestionData() {
if userDefaults.valueForKey("suggestions") != nil {
self.suggestions = userDefaults.valueForKey("suggestions") as? [String]
}
}
override func viewDidLoad() {
super.viewDidLoad()
getSuggestionData()
suggestions.insert("someString", atIndex: 0)
}
}
When i run this i get:
fatal error: unexpectedly found nil while unwrapping an Optional value
on the line where I try to insert a new object to the array.
I have tried following the approach in this thread, but it didn't save anything to the list.
I'm new to swift, and optional-types aren't my strong side, maybe someone know what's wrong?
As reading from user defaults could return nil values, use optional binding to be safe:
func getSuggestionData() {
if let suggestionArray = userDefaults.objectForKey("suggestions") {
self.suggestions = suggestionArray
} else {
self.suggestions = [String]()
}
}
But I'd recommend to use an non-optional variable with a defined default value.
In AppDelegate of your app, override init() or insert the code to register the key/value pair.
override init()
{
let defaults = NSUserDefaults.standardUserDefaults()
let defaultValue = ["suggestions" : [String]()];
defaults.registerDefaults(defaultValue)
super.init()
}
Registering the default keys and values is the way Apple suggests in the documentation.
If no value is written yet, the default value (empty array) is read.
If there is a value, the actual value is read
Instead of the value observer of the variable suggestions implement a method to insert a new value, delete the last one and write the array to disk.
There is no need to use optional binding because the array in user defaults has always a defined state.
let userDefaults = NSUserDefaults.standardUserDefaults()
var suggestions = [String]()
func insertSuggestion(suggestion : String)
{
if suggestions.count == 5 { suggestions.removeLast() }
suggestions.insert(suggestion, atIndex: 0)
userDefaults.setObject(suggestions, forKey: "suggestions")
userDefaults.synchronize()
}
func getSuggestionData() {
suggestions = userDefaults.objectForKey("suggestions") as! [String]
}
override func viewDidLoad() {
super.viewDidLoad()
getSuggestionData()
insertSuggestion("someString")
}
A side note:
never use valueForKey: rather than objectForKey: if you don't need explicitly the key-value-coding method.
Hey Just try like this way.
var suggestions: [String] = Array() {
didSet {
var arrayEnd = suggestions.count
if arrayEnd >= 5 {
arrayEnd = 4
}
if arrayEnd > 0
{
let newArray = Array(suggestions[0...(arrayEnd-1)])
userDefaults.setObject(newArray, forKey: "suggestions")
userDefaults.synchronize()
}
}
}
func getSuggestionData() {
if userDefaults.valueForKey("suggestions") != nil {
self.suggestions = userDefaults.objectForKey("suggestions") as! Array
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getSuggestionData()
if self.suggestions.count > 4
{
var str = "\(self.suggestions.count)"
self.suggestions.insert(str, atIndex: self.suggestions.count)
}
}

Resources