nsstring - Can't compare string entered through terminal to another string in Swift -
I am using the following code to implement the original dictionary using switching. Although the compiler is not returning any value, I do not know what the problem is, help is needed!
PS I'm new in Swift "RAHAN SUPRE", "JP": "JOI PATEL", "Junk":
import foundation var dyke = ["blue": "Neil Goswami", "Kiran": "Kina Shah", "Sapre" "Asha Shah"] Dick ["Asha Shah"], "Beta Shah", "Bhendi": "Shreya Bhat", "Ali Bhai": "Alsha Seth", "Gandhi": "Shlok Gandhi", "Binti" "Y"] = "Aishwarya Rawat" DIC ["Sikar"] = "Ruchir Patel" Dick ["Bhekhari"] = "Abha Singhal" Str: String? = "Initial" printline ("Enter Pet Name:") str = NSString (Data: NSFileHandle.fileHandleWithStandardInput (.data, encoding: NSUTF8StringEncoding) var st: String = str! For DIC (pet, work) (if (pet == cents) {println ("its real name is \")}}}}}
The problem is that the user input is new line character ( \ n ) in the string. It can be corrected by changing
var st: String = str!
to
var st = str ! .stringByTrimmingCharactersInSet (NSCharacterSet.newlineCharacterSet ())
Alternatively to the use
var st = str! .stringByTrimmingCharactersInSet (NSCharacterSet.whitespaceAndNewlineCharacterSet () ) < / Pre> which is a major and remove the rear space characters.
Note that you can make it easier to see in a dictionary to look:
if done = dic [st] {println ("his / her real name is (act)")}
Comments
Post a Comment