this is my first post here having downloaded Xcode last week. I have never programmed before and it all started off well. I cannot seem to access variables outside of functions in a project, but it does work in the playground. Am I doing something wrong? The code is attached below.
func givenValue(number: Int) -> (small: Int, large: Int)
{
let resultOne = (number*2)
let resultTwo = (number * number)
return (resultOne, resultTwo)
}
let answer = givenValue(15)
answer.small
answer.large
If i try this is the playground it all works well, if I attempt to use this in a project i get two errors as below:
"cannot invoke 'givenValue' with an argument list of type '(Int)' "
variable `"answer" flags up an expected declaration error.`
Thank you for any assistance, and my apologies if I formatted my question in the wrong way.
func givenValue(number: Int) -> (small: Int, large: Int)
{
let resultOne = (number*2)
let resultTwo = (number * number)
return (resultOne, resultTwo)
}
In ViewDidLoad Call it
override func viewDidLoad() {
super.viewDidLoad()
let answer = givenValue(15)
answer.small
answer.large
}
I hope it work!!!!!!
Related
I have a function I use to generate random strings for email addresses or passwords (for example). It was originally set like this:
static func random(length: Int) -> String {
let characters = "abcdefghijklmnopqrstuvwxyz"
return String((0..<length).map { _ in characters.randomElement()! })
}
So I changed it to this:
static func random(length: Int) -> String {
let characters = CharacterSet.lowercaseLetters
return String((0..<length).map { _ in characters.randomElement()! })
}
But I get an error saying "Value of type 'CharacterSet' has no member 'randomElement'.
I'm very new to Swift and I've done a lot of searching and so far I haven't found a good solution. I want to keep this function short and sweet. And I've been at this for a while now. Any help would be greatly appreciated! Please let me know if any more context is needed.
Edit: My question got closed because it was seen as a duplicate but, I looked at the solution page and tried to apply it to my issue and still no resolution. I'm not sure if that's because the previous answers are from 2015 and older or they were for obj-c
As I said in my comment of the possible duplicated post you can use that extension from the accepted answer to get all characters from a CharacterSet and get a randomElement from the resulting collection. Also as stated in the accepted answer some characters may not be present in the font used to display the result:
extension CharacterSet {
var array: [Character] {
var result: [Character] = []
for plane: UInt8 in 0...16 where hasMember(inPlane: plane) {
for unicode in UInt32(plane) << 16 ..< UInt32(plane + 1) << 16 {
if let uniChar = UnicodeScalar(unicode), contains(uniChar) {
result.append(Character(uniChar))
}
}
}
return result
}
}
let lowercaseLettersArray = CharacterSet.lowercaseLetters.array
let randomCharacter = lowercaseLettersArray.randomElement()! // "ᵳ"
I have integrated the FMDB Sqlite Wrapper into my iOS application and I have written a couple of DB functions, that have all worked perfectly until now.
I used the same approach for a simple method that is supposed to return a single element in a Resultset item, but I get the above error. There is nothing different in the logic from the other methods so I have no idea why the error occurs and why it happens on both lines with stringForColumn and not in the line with intForColumn (as the debugger tells me).
Any help or tips would be greatly appreciated!
func fetchExercise(exerciseId: Int) -> Exercise {
sharedInstance.database!.open()
let resultSet: FMResultSet! = sharedInstance.database!.executeQuery("Select * from Exercises where ExerciseId = ?", withArgumentsInArray: [String(exerciseId)])
let fetchedExercise: Exercise = Exercise()
if (resultSet != nil) {
fetchedExercise.exerciseId = Int(resultSet.intForColumn("ExerciseId"))
fetchedExercise.exerciseCategory = resultSet.stringForColumn("ExerciseCategory")
fetchedExercise.exerciseTitle = resultSet.stringForColumn("ExerciseTitle")
}
sharedInstance.database!.close()
return fetchedExercise
}
The resultSet is as follows:
You have to call resultsSet.next() to retrieve the results. For example:
func fetchExercise(exerciseId: Int) -> Exercise {
sharedInstance.database!.open()
defer { sharedInstance.database!.close() }
let resultSet = try! sharedInstance.database!.executeQuery("Select * from Exercises where ExerciseId = ?", values: [exerciseId])
let fetchedExercise = Exercise()
if resultSet.next() {
fetchedExercise.exerciseId = resultSet.longForColumn("ExerciseId")
fetchedExercise.exerciseCategory = resultSet.stringForColumn("ExerciseCategory")
fetchedExercise.exerciseTitle = resultSet.stringForColumn("ExerciseTitle")
}
return fetchedExercise
}
BTW, I'm not sure why you're converting exerciseId to a string in the query. I'd personally just pass the Int value, as shown above.
I am attempting to use Parse to call up some variables and put them into a struct that is already initialized. The calling of the variables is happening smoothly and the data is available, but the inputing of the class into the function is not happening.
'unit' is a struct that has the name, hp, attack, etc. variables contained within it.
Is it not possible to pass along an instance of a struct and modify it's values like this? It would save me a lot of copy-pasting code to do it this way.
Thanks for your help!
func fetchStats(name: String, inout nameOfClass: unit) {
var unitStatArray = []
let query = PFQuery(className: "UnitStats")
query.whereKey("name", equalTo: name)
query.findObjectsInBackgroundWithBlock{(objects:[PFObject]?, error: NSError?)->Void in
if (error == nil && objects != nil){ unitStatArray = objects! }
nameOfClass.name = "\(unitStatArray[0].objectForKey("name")!)"
print("class name is \(nameOfClass.name)")
print("cannon name is \(cannon.name)")
nameOfClass.hitPoints = unitStatArray[0].objectForKey("hitPoints") as! Double
nameOfClass.hitPointsMax = unitStatArray[0].objectForKey("hitPointsMax") as! Double
nameOfClass.attack = unitStatArray[0].objectForKey("attack") as! Double
nameOfClass.defense = unitStatArray[0].objectForKey("defense") as! Double
nameOfClass.rangedAttack = unitStatArray[0].objectForKey("rangedAttack") as! Double
nameOfClass.rangedDefense = unitStatArray[0].objectForKey("rangedDefense") as! Double
nameOfClass.cost = unitStatArray[0].objectForKey("cost") as! Int
}
}
fetchStats("3-inch Ordnance Rifle", nameOfClass: &cannon)
This is an attempt to explain what I had in mind when writing my comment above.
Because there's an asynchronous call to findObjectsInBackgroundWithBlock, the inout won't help you here. The idea is to add a callback fetched like this:
func fetchStats(name: String, var nameOfClass: unit, fetched: unit -> ()) {
// your code as above
query.findObjectsInBackgroundWithBlock {
// your code as above plus the following statement:
fetched(nameOfClass)
}
}
This can be called with
fetchStats("3-inch Ordnance Rifle", nameOfClass: cannon) { newNameOfClass in
nameOfClass = newNameOfClass
}
(all of this code has not been tested)
The point is that you understand that your code is asynchronous (I know, I'm repeating myself). After you have called fetchStats you don't know when the callback (here: the assignment nameOfClass = newNameOfClass) will be executed. You cannot assume the assignment has been done after fetchStats has returned.
So whatever you need to do with the changed nameOfClass: the corresponding statements must go into the callback:
fetchStats("3-inch Ordnance Rifle", nameOfClass: cannon) { newNameOfClass in
// do whatever you want with the received newNameOfClass
}
Hope this helps.
Yes there are other SO answers for the same error. But I cannot find a case that represents mine closely enough for me to figure it out. Please help.
These lines aren't related. They are in separate blocks. However they are giving the same error.
'array' is unavailable: please construct an Array from your lazy sequence: Array(...)
EDIT: These fixed thanks to Vadian. Correct code posted for whoever drops by :)
//let recordDic = YALCity.defaultContent().values.array[selectedIndexPath.row] // re-written
let recordArray = Array(YALCity.defaultContent().values)
let recordDic = recordArray[selectedIndexPath.row]
//let cityName = YALCity.defaultContent().keys.array[indexPath.row] // re-written
let cityArray = Array(YALCity.defaultContent().keys)
let cityName = cityArray[indexPath.row]
Fixed this one.
// for element in recordDic.keys.array { // error
for element in Array(recordDic.keys) // fixed :)
Still can't get this one.
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return YALCity.defaultContent().keys.array.count
} // error
OMG I got it!
return Array(YALCity.defaultContent().keys).count
The error message says that you cannot use array as a property. The suggested syntax is
let cityArray = Array(YALCity.defaultContent().keys)
let cityName = cityArray[indexPath.row]
I am working on Swift 1.2 with Xcode 6 but now I've installed Xcode 7 with Swift 2.1. There are many errors in Swift 2.1 and many syntax changes, even though my code works well with Swift 1.2. The first problem is with this method:
func getSubstringUpToIndex(index: Int, fromString str: String) -> String
{
var substring = ""
for (i, letter) in enumerate(str) {
substring.append(letter)
if i == index - 1 {
break
}
}
return substring
}
Another problem occurs on this line, "extra argument 'error' in call":
let jsonResult: Dictionary = NSJSONSerialization.JSONObjectWithData(self.mutableData, options: NSJSONReadingOptions.MutableContainers, error: &error) as! Dictionary<String, AnyObject>
In both cases it's Swift 1.2 code, not 2.0.
In the first snippet of code, the enumerate method does not exist anymore, you can update it this way:
func getSubstringUpToIndex(index: Int,
fromString str: String) -> String
{
var substring = ""
for (i, letter) in str.characters.enumerate() {
substring.append(letter)
if i == index - 1 {
break
}
}
return substring
}
Or this way, using substringRithRange :
func getSubstringUpToIndex2(index: Int,
fromString str: String) -> String {
let range = str.startIndex.advancedBy(0)..<str.startIndex.advancedBy(index)
return str.substringWithRange(range)
}
Try them out on swiftstub.
The second line you pasted does not work anymore because swift 2 now handles errors with try/do/catch, the last parameter NSErrorPointer does not exist anymore, learn more about swift2 error handling here.