Home > Mobile >  Novice Swift consulting two questions
Novice Swift consulting two questions

Time:09-26

Swift novice consulting two questions, problems, first sorry ha:)

1, because before learned Python, let the human feel Swift of string too collapse, so I write this function convenient pick string
Func cut_out_str (STR: String, the begin: Int, end: Int) - & gt; String {
Return (STR as nsstrings.) the substring (with: NSRange (location: the begin, end, end - the begin))
}
This kind of writing your own function to avoid standard code and naming using cut_out_str this Python function rather than cutOutStr this way of Swift function name is not the specification? If in the future as the APP stores, this way of writing code be shut from apple?


2, if such a text file

A1
B2
C3
.

Processing for such a two dimensional array [[" a ", "1"], [" b ", "2"], [" c ", "3"]]
If in python, simple to explode, oneself write a few lines of code can fix,
Gd=open (' e:/a.t xt ', 'r', encoding="utf-8")
Gdlist1=gd. Readlines ()

# delete/n
For I in range (0, (the len (gdlist1))) :
Gdlist1 [I]=gdlist1 [I] strip ()

# 1 d into 2 d
Main_list=[]
For I in gdlist1:
Main_list. Append ([I [0], I [1]])

But can not be in the Swift, array can only be the same data type, can only use the list of nested tuple way [(" a ", "1"), (" b ", "2"), (" c ", "3")], turned half a day on the Internet, is really couldn't find how to realize this simple function, Swift, use people so little? Thank you very much!

CodePudding user response:

Let arr: [String]=[" a1 ", "b2", "c3"]
Var res: [[String Element]]=[]

For strItem arr in {
Element var temp: [String]=[]
For char in strItem {
Temp. Append (char)
}
Res. Append (temp)
}

Print (res)

//[[" a ", "1"], [" b ", "2"], [" c ", "3"]]

  •  Tags:  
  • iOS
  • Related