Swift doesn't recognize recursive call to function, thinks it's a local variable [closed] - ios

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am following along with the Stanford Swift course on iTunes. I am in lesson 3 where the professor writes a recursive function to evaluate a stack of operators and operands.
Here is the code as the professor has typed it:
class CalculatorBrain
{
private enum Op {
case Operand(Double)
case UnaryOperation(String, Double -> Double)
case BinaryOperation(String, (Double, Double) -> Double)
}
private var opStack = [Op]()
private var knownOps = [String:Op]()
init() {
knownOps["×"] = Op.BinaryOperation("×", *)
knownOps["+"] = Op.BinaryOperation("+", +)
knownOps["-"] = Op.BinaryOperation("-") { $1 - $0 }
knownOps["÷"] = Op.BinaryOperation("÷") { $1 / $0 }
knownOps["√"] = Op.UnaryOperation("√", sqrt)
}
func pushOperand(operand: Double) {
opStack.append(Op.Operand(operand))
}
func performOperation(symbol: String) {
if let operation = knownOps[symbol] {
opStack.append(operation)
}
}
private func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op])
{
var remainingOps = ops
if !remainingOps.isEmpty {
let op = remainingOps.removeLast()
switch op {
case .Operand(let operand):
return (operand, remainingOps)
case .UnaryOperation(_, let operation):
let operandEvaluation = evaluate(remainingOps)
if let operand = operandEvaluation.result {
return (operation(operand), operandEvaluation.remainingOps)
}
case .BinaryOperation(_, let operation):
// code snipped
}
}
return (nil, ops)
}
}
I get a compiler error but he doesn't.
The error I get is on the line let operandEvaluation = evaluate(remainingOps). I get the error "Use of local variable 'evaluate' before its declaration"
Does anyone know why I'm getting this error? It's supposed to be a recursive call to the function evaluate, but instead the compiler thinks evaluate is a local variable.
I am using Xcode 6.4 on OS X 10.10.4
Thanks!

I got this error, too.
However, I fixed it by adding my bracket to the if clause several lines below that error.

You're missing the } to close:
if let operand = operandEvaluation.result {
And the the switch is not exhaustive, you need to add the missing case .BinaryOperation.

There was a bracketing typo after all. Sorry, and thanks for the assistance.

Related

getting crash on null data with callback function [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Here is my code:
func getProduct(productId: String, callback: #escaping (String) -> () ){
Transport.sharedInstance.getProduct(productId: productId) { (data, err) in
if let _ = err{
}else{
if let dt = data as? String{
let pro = Mapper<SubCategoryProducts>().map(JSONString: dt)
let com = ComboData()
com.price = pro?.Price ?? 0
com.type = pro?.type ?? ""
com.amount = pro?.quantity ?? ""
if (pro?.combodata == nil) {
pro?.combodata?.append(com)
} else if ((pro?.combodata?.contains(where: {$0.type == pro?.type})))! {
} else {
pro?.combodata?.append(com)
}
self.arrayForCombo.removeAll()
pro?.combodata?.forEach({ (com) in
let str = "\(com.amount) " + "\(com.type)" + " - " + "₹ \(com.price)"
self.arrayForCombo.append(str)
})
callback(self.arrayForCombo[0])
}
}
}
}
I am getting nil pro?.combodata so only I have checked wether I am getting nil value or not and then when it comes to callback it getting crash on here callback(self.arrayForCombo[0]). Because its not appending the values. I don't know why, but not able to achieve that. Any solution it would be helpful.
Check if forEach is even stepped into.
At this point it can be that either pro or pro.combodata are nil and forEach is not called on nil collection.
Probably you're trying to access [0] index element after self.arrayForCombo.removeAll().
Try using this:
guard let pro = Mapper<SubCategoryProducts>().map(JSONString: dt) else { return }
let com = ComboData()
com.price = pro.Price ?? 0
com.type = pro.type ?? ""
com.amount = pro.quantity ?? ""
if (pro.combodata == nil) {
pro.combodata = [com]
}
The problem is that when combodata of pro is nil than you are trying to append the object that means you are trying nil.append() while it should be [ComboData].append().
I hope this will help you.
When the optional array is nil then calling append will do nothing, you need to initialize it first
if (pro?.combodata == nil) {
pro?.combodata = []
pro?.combodata?.append(com)
}
After executing self.arrayForCombo.removeAll() statement, arrayForCombo become empty. If your pro?.combodata is nil then forEach statement could not execute. So still your arrayForCombo is empty.
But in the callBack statement you forcefully try to access the first item of arrayForCombo whether it's empty or not. That's why it crushes when the array is empty.
You can safely by pass the cruhes like that:
if let firstItem = arrayForCombo.first {
callback(firstItem)
}

When to return a function from another function in Swift [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
What it the most common situation where you would want to return a function from a function in Swift?
In the code below I'm returning a function but I don't really see the purpose since the function I'm returning is inside the function who is returning it. The reason I'm confused is because we could accomplish the same thing with just one function.
func person () -> ((String, Int) -> String) {
func info(name: String, age: Int) -> (String) {
return "\(name) is \(age) old"
}
return info
}
let nathan = person()
nathan("Nathan", 3)
print(nathan("Nathan", 3))
Can someone point out common situations where you would want to return a function and probably illustrate it with a better example?
I want to understand this since this is fundamental for programming in general not just Swift (I think).
A classic example would be in a calculator program, e.g.:
func operatorForString(str: String) -> ((Float, Float) -> Float)? {
if str == "+" {
return (+) // brackets required to clarify that we mean the function
} else if str == "-" {
return (-)
} else if str == "*" {
return (*)
} else if str == "/" {
return (/)
} else if str == "**" {
return pow // No brackets required here
} else {
return nil
}
}
if let op = operatorForString("-") {
let result = op(1, 2) // -1
}
It's rather contrived, but it illustrates the principle simply...
As an "exercise to the reader" try to do it as a Dictionary lookup, rather than repeated ifs :)

iOS 9 Stanford Course in Swift - Lecture 1

I'm currently trying to complete the Swift Course on iTunes U and we are building a calculator. I'm having trouble understanding part of the code.
I added the code below that I thought was relevant from the file.
Here is what confuses me: why does operation(operand) compute the value for the UnaryOperation (i.e. the square root)? I see that when the CalculatorBrain class is called the dictionary is initialized, but when I print the dictionary out I just get something that looks like this: [✕: ✕, -: -, +: +, ⌹: ⌹, √: √]. So where/when does the program compute the square root when I click on the square root button?
Class CalculatorBrain
{
private enum Op: Printable
{
case Operand(Double)
case UnaryOperation(String, Double -> Double)
case BinaryOperation(String, (Double, Double) -> Double)
var description: String {
get {
switch self {
case .Operand(let operand):
return "\(operand)"
case .UnaryOperation(let symbol, _):
return symbol
case .BinaryOperation(let symbol, _):
return symbol
}
}
}
}
private var opStack = [Op]()
private var knownOps = [String: Op]()
init() {
func learnOp(op: Op) {
knownOps[op.description] = op
}
learnOp(Op.BinaryOperation("✕", *))
learnOp(Op.BinaryOperation("⌹") { $1 / $0 })
learnOp(Op.BinaryOperation("+", +))
learnOp(Op.BinaryOperation("-") { $0 - $1 })
learnOp(Op.UnaryOperation ("√", sqrt))
}
private func evaluate(ops: [Op]) -> (result: Double?, remainingOps: [Op])
{
if !ops.isEmpty {
var remainingOps = ops
let op = remainingOps.removeLast()
switch op {
case .Operand(let operand):
return (operand, remainingOps)
case .UnaryOperation(_, let operation):
let operandEvaluation = evaluate(remainingOps)
if let operand = operandEvaluation.result {
**return (operation(operand), operandEvaluation.remainingOps)**
}
// case.BinaryOperation(.....)
}
}
return (nil, ops)
}
func evaluate() -> Double? {
let (result, remainder) = evaluate(opStack)
return result
}
func pushOperand(operand: Double) -> Double? {
opStack.append(Op.Operand(operand))
return evaluate()
}
func performOperation(symbol: String) -> Double? {
if let operation = knownOps[symbol] {
opStack.append(operation)
}
return evaluate()
}
}
The Op enum implements the Printable protocol, which means it has a description: String property. When you print the Dictionary, you are sending [String : Op] to the println function which then tries to print the Op using its description.
The reason the description of the operators is the same as its key in the Dictionary is because the learnOp(op: Op) function sets the key to be op.description (knownOps[op.description] = op)
To see the effects of this, you could add a new operator learnOp(Op.UnaryOperation ("#", sqrt)) which will be printed as #:# inside of the knownOps Dictionary. (And if you add a new button for the # operator, it will also perform the square root operation)
Since the calculator is stack based, the operands get pushed on, then the operations. When evaluate() gets called, it calls evaluate(opStack) passing the entire stack through.
evaluate(ops: [Op]) then takes the to item off of the stack and evaluates the function after having calculated the operands.
As an example, lets say you want to calucalte sqrt(4 + 5).
You would push the items onto the stack, and it would look like: [ 4, 5, +, sqrt ]
Then evaluate(ops: [Op]) sees the sqrt and evaluates the operand with a recursive call. That call then evaluates + with two more recursive calls which return 5 and 4.
The tree of calls would look like this:
ops: [4, 5, +, sqrt] // Returns sqrt(9) = 3
|
ops: [4, 5, +] // Returns 4 + 5 = 9
____|_____
| |
ops: [4, 5] ops: [4]
return 5 return 4
I strongly recommend you put a breakpoint on the evaluate() -> Double? function and step through the program to see where it goes with different operands and operations.
learnOp(Op.UnaryOperation ("√", sqrt))
sqrt is a built in function, so you're teaching the calculator that "√" means it should perform the sqrt operation.

Return statement skipped in Swift

I'm trying to teach myself Swift via the Stanford iTunes U course (currently working on the calculator app), but I just ran into a super weird issue that I've never seen before and can't figure out how to solve: one of my return statements (and only the return statement itself) is being skipped during runtime.
func evaluateOps(ops: [Op]) -> (result: Double?, remainingOps: [Op]) {
if !ops.isEmpty {
var remainingOps = ops
let op = remainingOps.removeLast()
switch op {
case ...
case ...
case .BinaryOperation(_, let operation):
// check that there are 2 operands before it
if remainingOps.count == 2 {
let op1Evaluation = evaluateOps(remainingOps)
if let operand1 = op1Evaluation.result {
let op2Evaluation = evaluateOps(op1Evaluation.remainingOps)
if let operand2 = op2Evaluation.result {
// PROBLEM AREA...
let x = (operation(operand1, operand2), op2Evaluation.remainingOps)
println("results: \(x.0) remainder: \(x.1)")
return (x.0, x.1) // skipped during runtime...
}
}
}
else { ... }
}
}
println("returning nil")
return (nil, ops)
}
// troublesome method is called here...
let (result, remainder) = evaluateOps(opStack)
println("\(opStrings) = \(result) with \(remainderStrings) leftover")
Everything works so that, if I tried to calculate 5*3 for example, the console would read:
results: 15.0 remainder: []
returning nil
[5.0, ×, 3.0] = nil with [5.0, ×, 3.0] leftover
I think the problem might have something to do with the fact that, in the above code, if I tried to simply return x, I get a compile error that reads Cannot express tuple conversion '(Double, [CalculatorModel.Op])' to '(result: Double?, remainingOps: [CalculatorModel.Op])'. I also have no idea what to do with this.
In my researching the problem, I've discovered the downcasting keyword as (see altered code below), which removed the compile error when returning x rather than (x.0, x.1) but results in the same console result (except that now it says results: Optional(15.0) remainder: []):
let x = (operation(operand1, operand2) as Double?, op2Evaluation.remainingOps as [Op])
println("results: \(x.0) remainder: \(x.1)")
return x // no error but same result as return (x.0, x.1)
I've also tried sticking a println(getClassName(x.0!)) just before the return statement to make sure I even had a double, and it spit out __NSCFNumber... which I also researched, and found to be highly underdocumented (or at least not documented well enough to figure out how that's affecting me or how I can fix it!)
Although as you can see, it should be a double...
enum Op {
case Operand(Double)
case UnaryOperation(String, Double -> Double)
case BinaryOperation(String, (Double, Double) -> Double)
var description: String {
get {
switch self {
case .Operand(let operand):
return "\(operand)"
case .UnaryOperation(let symbol, _):
return "\(symbol)"
case .BinaryOperation(let symbol, _):
return "\(symbol)"
}
}
}
}
As you can see, everything's working perfectly fine as it's performing the calculation and everything...until it gets to that little troublesome area. I've searched StackOverflow and did a bit of Googling, but this seems to be a rather uncommon problem... Any help is appreciated! Let me know if you need any further information. Thanks!
EDIT:
getClassName(_:) is defined below (found it online somewhere...):
func getClassName(obj : AnyObject) -> String
{
let objectClass : AnyClass! = object_getClass(obj)
let className = objectClass.description()
return className
}

Swift switch statement for matching substrings of a String

Im trying to ask for some values from a variable.
The variable is going to have the description of the weather and i want to ask for specific words in order to show different images (like a sun, rain or so)
The thing is i have code like this:
if self.descriptionWeather.description.rangeOfString("Clear") != nil
{
self.imageWeather.image = self.soleadoImage
}
if self.descriptionWeather.description.rangeOfString("rain") != nil
{
self.imageWeather.image = self.soleadoImage
}
if self.descriptionWeather.description.rangeOfString("broken clouds") != nil
{
self.imageWeather.image = self.nubladoImage
}
Because when i tried to add an "OR" condition xcode gives me some weird errors.
Is it possible to do a swich sentence with that? Or anyone knows how to do add an OR condition to the if clause?
I had a similar problem today and realized this question hasn't been updated since Swift 1! Here's how I solved it in Swift 4:
switch self.descriptionWeather.description {
case let str where str.contains("Clear"):
print("clear")
case let str where str.contains("rain"):
print("rain")
case let str where str.contains("broken clouds"):
print("broken clouds")
default:
break
}
Swift 5 Solution
func weatherImage(for identifier: String) -> UIImage? {
switch identifier {
case _ where identifier.contains("Clear"),
_ where identifier.contains("rain"):
return self.soleadoImage
case _ where identifier.contains("broken clouds"):
return self.nubladoImage
default: return nil
}
}
You can do this with a switch statement using value binding and a where clause. But convert the string to lowercase first!
var desc = "Going to be clear and bright tomorrow"
switch desc.lowercaseString as NSString {
case let x where x.rangeOfString("clear").length != 0:
println("clear")
case let x where x.rangeOfString("cloudy").length != 0:
println("cloudy")
default:
println("no match")
}
// prints "clear"
Swift language has two kinds of OR operators - the bitwise ones | (single vertical line), and the logical ones || (double vertical line). In this situation you need a logical OR:
if self.descriptionWeather.description.rangeOfString("Clear") != nil || self.descriptionWeather.description.rangeOfString("clear") != nil {
self.imageWeather.image = self.soleadoImage
}
Unlike Objective-C where you could get away with a bitwise OR in exchange for getting a slightly different run-time semantic, Swift requires a logical OR in the expression above.
If you do this a lot, you can implement a custom ~= operator that defines sub-string matching. It lends itself to this nice syntax:
switch "abcdefghi".substrings {
case "def": // calls `"def" ~= "abcdefghi".substrings`
print("Found substring: def")
case "some other potential substring":
print("Found \"some other potential substring\"")
default: print("No substring matches found")
}
Implementation:
import Foundation
public struct SubstringMatchSource {
private let wrapped: String
public init(wrapping wrapped: String) {
self.wrapped = wrapped
}
public func contains(_ substring: String) -> Bool {
return self.wrapped.contains(substring)
}
public static func ~= (substring: String, source: SubstringMatchSource) -> Bool {
return source.contains(substring)
}
}
extension String {
var substrings: SubstringMatchSource {
return SubstringMatchSource(wrapping: self)
}
}
I'd recommend using a dictionary instead, as a mapping between the substring you're searching for and the corresponding image:
func image(for weatherString: String) -> UIImage? {
let imageMapping = [
"Clear": self.soleadoImage,
"rain": self.soleadoImage,
"broken clouds": self.nubladoImage]
return imageMapping.first { weatherString.contains($0.key) }?.value
}
A dictionary gives you flexibility, adding new mappings is easy to do.
This link also describes overloading operator ~= which is actually used by the switch statement for matching cases to allow you to match regular expressions.

Resources