String Shown as Unicode - Swift [duplicate] - ios

This question already has answers here:
Saving Hebrew text to NSUserDefaults return strange encoding
(3 answers)
Closed 7 years ago.
When I print a String array to the log most of the Strings show up like they're suppose to, but sometimes the show like this:
\U200e\U05d3\U05d5\U05e8\U05d9\U05ea \U05dc\U05d5\U05d9\U200e
What is causing this problem?

When you log an array with NSLog, or when you log an NSArray with print, you are relying on Cocoa's logging. It's very old so it represents a non-ASCII string by showing its codepoints.
If you don't like that, log with Swift's print and make sure you are logging a Swift Array, not an NSArray.
let s = "\u{200e}\u{05d3}\u{05d5}\u{05e8}\u{05d9}\u{05ea}"
NSLog("%#",[s]) // Cocoa is logging
print([s] as NSArray) // Cocoa is logging
print([s]) // Swift is logging

Related

UUIDWithString crash when input invalid param [duplicate]

This question already has answers here:
How to check the validity of a GUID (or UUID) using NSRegularExpression or any other effective way in Objective-C
(4 answers)
Closed 3 years ago.
When I use [CBUUID UUIDWithString:#"xxx"], something like that the invalid uuid string, the app will crash.
How to check if the param is a valid UUID-format.
The error is:
Assertion failure in -[CBUUID initWithString:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/MobileBluetoothFramework/MobileBluetooth-115.5.1/CoreBluetooth/CoreBluetooth/CBUUID.m:149
I can't understand why the assertion happens on release version. Maybe Apple forget to turn off assertion switch.
Try this:
-(BOOL)isUUID:(NSString *)string
{
return !![[NSUUID alloc] initWithUUIDString:string];
}

How to store and retrive emojis in the Firebase using Swift 2.0+

Hi anyone can please help me how to store Emojis in Firebase? I am trying like below but the app is crashing while i run.
let emoji = emoji.unicodeScalars
USER_REF.childByAppendingPath(friendUID as String).childByAppendingPath("messages").setValue(emoji, forKey: "emoji")
My intention here was converting an emoji into unicode, and saving in to the firebase database.
Emoticons are unicode characters and can be stored by their actual value. So for example a smile face is 1F642.
There's a wiki on that topic it has a chart and cross reference on it; search for Emoticons (Unicode_block)
To save to Firebase
let smiley = "\U0001F642"
smileyRef.setValue: smiley
To read from Firebase:
Read the value from Firebase and stuff it into a string that will allow it to be a unicode char. Then to present the string UI, assign that string to a text field that supports unicode; NSTextField in this case.
self.myTextField.stringValue = smiley;

Swift warnings in button action function [duplicate]

This question already has an answer here:
"Initialisation of immutable value'context' was never used, consider replacing assignment to '_' or removing it
(1 answer)
Closed 7 years ago.
I am trying to set the signup button so that when the user taps the signup button it will read the read user email and the user password. So I entered this code to declare my variables but Swift keeps giving me this warning:> "initialization of immutable value 'userEmail' was never used, consider replacing with assignment to '_' or removing it." for all the variables/Constant I try to enter.
code:
#IBAction func SignupButtonTapped(sender: AnyObject) {
let userEmail = userEmailTextfield.text;
let userUsername = userUsernameTextfield.text;
let userPassword = userPasswordTextfield.text;
Below is an image view of the problem.
The error message is quite clear.
initialization of immutable value 'userEmail' was never used.
The warning goes away when you're going to write code for the to-do line
// Store Data
for example by comparing a string with the constant or assigning it to something.

Swift - which types to use? NSString or String

With the introduction of Swift I've been trying to get my head round the new language
I'm an iOS developer and would use types such as NSString, NSInteger, NSDictionary in an application. I've noticed that in the "The Swift Programming Language" ebook by Apple, they use the Swift types String, Int, Dictionary
I've noticed the Swift types don't have (or are differently named) some of the functions that the Foundation types do. For example NSString has a length property. But I've not been able to find a similar one for the Swift String.
I'm wondering, for an iOS application should I still be using the Foundation types?
You should use the Swift native types whenever possible. The language is optimized to use them, and most of the functionality is bridged between the native types and the Foundation types.
While String and NSString are mostly interchangeable, i.e, you can pass String variables into methods that take NSString parameters and vice versa, some methods seem to not be automatically bridged as of this moment. See this answer for a discussion on how to get the a String's length and this answer for a discussion on using containsString() to check for substrings. (Disclaimer: I'm the author for both of these answers)
I haven't fully explored other data types, but I assume some version of what was stated above will also hold true for Array/NSArray, Dictionary/NSDictionary, and the various number types in Swift and NSNumber
Whenever you need to use one of the Foundation types, you can either use them to type variables/constants explicitly, as in var str: NSString = "An NSString" or use bridgeToObjectiveC() on an existing variable/constant of a Swift type, as in str.bridgeToObjectiveC().length for example. You can also cast a String to an NSString by using str as NSString.
However, the necessity for these techniques to explicitly use the Foundation types, or at least some of them, may be obsolete in the future, since from what is stated in the language reference, the String/NSString bridge, for example, should be completely seamless.
For a thorough discussion on the subject, refer to Using Swift with Cocoa and Objective-C: Working with Cocoa Data Types
NSString : Creates objects that resides in heap and always passed by reference.
String: Its a value type whenever we pass it , its passed by value.
like Struct and Enum, String itself a Struct in Swift.
public struct String {
// string implementation
}
But copy is not created when you pass. It creates copy when you first mutate it.
String is automatically bridged to Objective-C as NSString. If the Swift Standard Library does not have, you need import the Foundation framework to get access to methods defined by NSString.
Swift String is very powerful it has plethora of inbuilt functions.
Initialisation on String:
var emptyString = "" // Empty (Mutable)
let anotherString = String() // empty String immutable
let a = String(false) // from boolean: "false"
let d = String(5.999) // " Double "5.99"
let e = String(555) // " Int "555"
// New in Swift 4.2
let hexString = String(278, radix: 18, uppercase: true) // "F8"
create String from repeating values:
let repeatingString = String(repeating:"123", count:2) // "123123"
In Swift 4 -> Strings Are Collection Of Characters:
Now String is capable of performing all operations which anyone can
perform on Collection type.
For more information please refer apple documents.
Your best bet is to use Swift native types and classes, as some others have noted NSString has toll free translation to String, however, they're not the same a 100%, take for example the following
var nsstring: NSString = "\U0001F496"
var string: String = "\U0001F496"
nsstring.length
count(string)
you need to use the method count() to count the characters in string, also note that nsstring.length returns 2, because it counts its length based on UTF16.
Similar, YES
The same, NO
String and NSString are interchangeable, so it doesn't really matter which one you use. You can always cast between the two, using
let s = "hello" as NSString
or even
let s: NSString = "hello"
NSInteger is just an alias for an int or a long (depending on the architecture), so I'd just use Int.
NSDictionary is a different matter, since Dictionary is a completely separate implementation.
In general I'd stick to swift types whenever possibile and you can always convert between the two at need, using the bridgeToObjectiveC() method provided by swift classes.
Swift 4 update
String gets revisions in swift 4. Now you can directly call count on it and it consider grapheme clusters as 1 piece, like an emoji. NSString is not updated and is counting it in another way.
var nsstring: NSString = "๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ"
var string: String = "๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ"
print(nsstring.length) // 11
print(string.count) // 1
Since the objective C types are still dynamically dispatched they're probably going to be slower. I'd say you're best served using the Swift native types unless you need to interact with objective-c APIs
Use the Swift native types whenever you can. In the case of String, however, you have "seamless" access to all the NSString methods like this:
var greeting = "Hello!"
var len = (greeting as NSString).length
Swift Strings are quite elegant and easy to use, unless you need to parse them. The whole concept of indexing into Swift strings is just plain crazy. Whenever I need to parse through a typical unicode string, I convert it to NSString. Swift makes this a bit tricky though in that the common "character" integer expressions like ' ' or 'A' or '0' don't work in Swift. You have to use 32, 65, 48. Unfortunately, I'm not kidding! Because of this, I've put most of my string parsing code into an NSString extension written in Objective-C.
Yes I do know WHY Swift's designers made String indexing so crazy: They wanted to be able to express many-byte characters like emoji as single "Characters". My choice would have been to let this rare use case be expressed as multiple UTF16 characters, but what the heck.
String is a struct
// in Swift Module
public struct String
{
}
NSString is a class
// in Foundation Module
open class NSString : NSObject
{
}

Convert Arabic NSString to Decode string in IOS [duplicate]

This question already has answers here:
How do I URL encode a string
(24 answers)
Closed 8 years ago.
How to convert the Arabic text like http://www.someurl.com/Category/ุตููˆู to http://www.someurl.com/Category/%D8%B5%D9%81%D9%88%D9%81 , Like when we paste the http://www.someurl.com/Category/ุตููˆู to any browser it will show http://www.someurl.com/Category/%D8%B5%D9%81%D9%88%D9%81.
many solutions available
try this page first solution and solution no (17) they've what you're looking 4
How do I URL encode a string

Resources