I have an issue with converting character type to String type. First of all, I have below extension of String for finding nth character within String.
extension String { func characterAtIndex(index: Int) -> Character? { var cur = 0 for char in self { if cur == index { return char } cur++ } return nil } }
I get what I want with this class extension. However when I use that nth character for title of my custom UIButton, gives an error. My Uibutton Class is
class hareketliHarfler: UIButton { init(frame: CGRect) { super.init(frame: frame) // Initialization code } func getLetter(letter:String!){ self.titleLabel.text = letter } }
The error show when i try to access "getLetter(letter:String)" function. Here is example of main view Controller codes:
var harfim = hareketliHarfler(frame: CGRectMake(100,100,100,100)) var str="This is my String" var bufi=str.characterAtIndex(3) harfim.getLetter(bufi as AnyObject) ****
In * section I try .getLetter(bufi), .getLetter(bufi as String) also I try to change parameter type of function. Look like: func getLetter(letter:Character!) or func getLetter(letter:AnyObject!)...etc Didn't find a way. Need a help on that. Thank you
To convert an Int value to a String value in Swift, use String(). String() accepts integer as argument and returns a String value created using the given integer value.
this should be as simple as let characterFromString = Character(textField. text) . NSString is automatically bridged to Swift's String , and the Character class has an init which accepts a single character String ; see the documentation here.
Overview. A CharacterSet represents a set of Unicode-compliant characters. Foundation types use CharacterSet to group characters together for searching operations, so that they can find any of a particular set of characters during a search.
Element. A type representing the sequence's elements.
How about the simple String(theCharacter)
Works in Swift 4 and Swift 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With