String and Character are data type. Mutability with var Keyword. Strings are value type(not object or ref kind). (Value types mean type of structure or basic type , object/ref kind means type of class object/ref)
Every string is composed of encoding-independent Unicode characters and provides support for accessing those characters in various Unicode representations.
Swift String class is bridged with NSString. all function of NSString available when typecast to NSString.
When you pass string value to function then each case new copy of existing string value is created and new copy is passed or assigned. Becoz String is value type not reference type.
Swift’s compiler optimizes string usage so that actual copying takes place only when absolutely necessary.
Its valid: let exclamationMark: Character = "!" // string to character.
Obj-C Stringwithformat like : let message = "\(multiplier) times 2.5 is \ (Double(multiplier) * 2.5)”
Get character from string with characters property becoz they can not access directly as its encoding independent unicode characters.
Unicode scalar is unique 21 bit character and swift’s native string type is built from unicode scalar values.
usage of same :
let eAcute: Character = "\u{E9} // é
let combinedEAcute: Character = "\u{65}\u{301}" // e followed by ́ (This is called extended grapheme cluster as combination of multiple character in one)
As seen two different way for same character but character count for both is same i,e. one. But actual amount of memory required is different.
The count of the characters returned by the characters property is not always the same as the length property of an NSString that contains the same characters.
The length of an NSString is based on the number of 16-bit code units within the string’s UTF-16 representation and not the number of Unicode extended grapheme clusters within the string.”
Accessing string with . startIndex, endIndex, predecessor, sucessor , advancedBy methods.
let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.endIndex.predecessor()]
// !
greeting[greeting.startIndex.successor()]
// u
let index = greeting.startIndex.advancedBy(7)
greeting[index]s
// a
Compare string with == and != operators.
When unicode string written to file then it gets converted to either Utf8 or utf16 or utf32 bit.
0 comments:
Post a Comment