ios


issue with passing array of dictionaries into function


Swift is new for me so I would appreciate your help.
I have an array:
let bankDict = [
["bank": "b1", "currency": "c1", "buyrate": 0.11, "sellrate": 1.222, "officialrate": 2.15],
["bank": "b1", "currency": "c2", "buyrate": 3.11, "sellrate": 4.222, "officialrate": 5.15],
["bank": "b1", "currency": "c3", "buyrate": 7.11, "sellrate": 8.222, "officialrate": 9.15]]
I am trying to create a function which should return buyrate/sellrate/officialrate from my array bankDict.
So far I have created just a loop to test logic:
for dict in bankDict {
let b = dict["bank"]!
let c = dict["currency"]!
if b == "b1" && c == "c2" {
let br = dict["buyrate"]!
let sr = dict["sellrate"]!
let or = dict["officialrate"]!
println("\(br) -> \(sr) -> \(or)")
}
}
As expected returns me: 3.11 -> 4.222 -> 5.15
When I am trying to create function:
func showRates (bnk: String, crnc: String) -> (br: Double, sr: Double, or: Double) {
for dict in bankDict {
let b = dict["bank"]!
let c = dict["currency"]!
if b == bnk && c == crnc {
let br = dict["buyrate"]
let sr = dict["sellrate"]
let or = dict["officialrate"]
println("\(br) -> \(sr) -> \(or)")
}
return (br, sr, or)
}
}
I get an error: use of unresolved identifier 'sr'
It's an issue of scope. br, sr, and or were defined in the scope of the if-statement:
if b == bnk && c == crnc {
let br = dict["buyrate"]
let sr = dict["sellrate"]
let or = dict["officialrate"]
println("\(br) -> \(sr) -> \(or)")
}
Therefore they are only useable within that block of code.
If you move the return statement to be part of the if-statement it works (with a few other adjustments):
let bankDict = [
["bank": "b1", "currency": "c1","buyrate": 0.11, "sellrate": 1.222, "officialrate": 2.15],
["bank": "b1", "currency": "c2","buyrate": 3.11, "sellrate": 4.222, "officialrate": 5.15],
["bank": "b1", "currency": "c3","buyrate": 7.11, "sellrate": 8.222, "officialrate": 9.15]]
func showRates(bank: String, currency: String) -> (buyrate: Double, sellrate: Double, officialrate: Double)? {
for dict in bankDict {
if dict["bank"] == bank && dict["currency"] == currency {
if let buyrate = dict["buyrate"] as? Double,
let sellrate = dict["sellrate"] as? Double,
let officialrate = dict["officialrate"] as? Double {
return (buyrate, sellrate, officialrate)
}
}
}
return nil
}
SwiftStub-Code (not the Swift 1.2 version yet)
Have you think what will happen if b is not equals to bnk or c to crnc? What should your function need to return in that case? If you want to return nil values then you can do the following
func showRates (bnk: String, crnc: String) -> (br: Double?, sr: Double?, or: Double?) {
for dict in bankDict {
var b = dict["bank"]!
var c = dict["currency"]!
if b == bnk && c == crnc {
var br = dict["buyrate"] as? Double
var sr = dict["sellrate"] as? Double
var or = dict["officialrate"] as? Double
println("\(br) -> \(sr) -> \(or)")
return (br, sr, or)
}
}
return (nil, nil, nil)
}

Related Links

NSURL works on initial load For AVPlayer but will not load on next run, why?
Type MyViewController does not conform to protocol 'STPPaymentContextDelegate'
NSBatchDeleteRequest in Swift 3 for iOS 10 only?
Must I wait for Alamofire to finish post request or is it all asynchronous
Check if user has auto-correction enabled iOS
How can I open my app using openUrl schema from app extension?
Increase Badge Count From Client side not from Payload Receive count
How to update objects in core data?
Uploading video using AFNetworking causes memory issue
How whatsapp calling works in IOS?
Navigating to UITableViewController embedded with UINavigationController
Swift - return variable from within closure
CoreData: Observing Property Changes Behaves Differently
App sometimes crashed on start
How can I avoid the preview of password UITextField? [duplicate]
How to change background color of UIWebBrowserView in UIWebView in iOS8 Objective C

Categories

HOME
f#
jsp
whatsapp
identityserver4
openshift-origin
order
datetime
garbage-collection
bootstrap-selectpicker
tsql
weblogic11g
mvvm-light
pyusb
entity
android-vision
superfish
obiee
proftpd
jgit
countdown
pywin32
graphcool
thrift
curve-fitting
freetype
windows-applications
pcap
jet
sencha-touch-2
amazon-cloudfront
line-breaks
background-subtraction
epicorerp
azure-redis-cache
structure
mediacodec
reformatting
opensuse
stl
clappr
cloudinary
google-shared-contacts
macvim
microsoft-translator
otroslogviewer
visualsvn-server
logicblox
syncfusion
wp-api
gretl
invalidate
arm7
xbrl
koala-framework
stroke
database-cleaner
dump
ios-app-group
glfw
ice
playback
hipi
autogen
curly-braces
sizing
ellipsis
vb6-migration
intrinsics
java-gstreamer
membership
genetic
biojava
symbian
wt
multiscreen
iphone-5
scalariform
antiforgerytoken
llvm-gcc
openxls
swift-array
puppet-enterprise
horizontalscrollview
concurrentdictionary
subdirectory
dataformat
youtrack-api
disk-io
cfile
shift
gdb-python
chromeless
wescheme
android-actionbar-compat
vim-fugitive
settings.bundle
orthographic
executescalar
polyglot
time-estimation
run-length-encoding
xlink
mud
large-teams
metaphone

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App