Saturday, 15 August 2015

ios - Swift checking String for guid value -



ios - Swift checking String for guid value -

i receive web service authorisation string, represents guid (c#). how can convert string guid using swift? or, how can validate result, is, in fact, guid?

var str1:string = "5810744d-49f7-4edc-aefb-ecd1ebf9e59b" var str2:string = "some text"

how can define - string contains guid?

you can utilize nspredicate regex see if string in right format:

var str1:string = "(5810744d-49f7-4edc-aefb-ecd1ebf9e59b)" var str2:string = "some text" allow guidpred = nspredicate(format: "self matches %@", "((\\{|\\()?[0-9a-f]{8}-?([0-9a-f]{4}-?){3}[0-9a-f]{12}(\\}|\\))?)|(\\{(0x[0-9a-f]+,){3}\\{(0x[0-9a-f]+,){7}0x[0-9a-f]+\\}\\})") // prints "str1 guid" if guidpred.evaluatewithobject(str1) { println("str1 guid") } else { println("str1 not guid") } // prints "str2 not guid" if guidpred.evaluatewithobject(str2) { println("str2 guid") } else { println("str2 not guid") }

this regex match of 4 formats listed on msdn. maintain (relatively) simple, look match ill-formatted strings (such if delete 1 hyphen, not others: 5810744d49f7-4edc-aefb-ecd1ebf9e59b), filter out regular text.

ios swift

No comments:

Post a Comment