Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: How to get the value from a AutoreleasingUnsafePointer<NSString?> from a NSScanner?

I don't know how to use the AutoreleasingUnsafePointer. I have the following code:

var myString: AutoreleasingUnsafePointer<NSString?>

myScanner.scanUpToCharactersFromSet(NSCharacterSet.newlineCharacterSet(), intoString: myString)

Now, what should I do to get the string from 'myString'? I know it can be nil, but I couldn't figure out a way to get the string value in case it wasn't nil. And the Swift unwrapping technique just only works with the Optional type.

Thanks!

like image 351
Zaxonov Avatar asked Jun 13 '14 12:06

Zaxonov


People also ask

Are string and character comparisons in Swift locale-sensitive?

The characters are visually similar, but don’t have the same linguistic meaning: print ( "These two characters aren't equivalent." ) // Prints "These two characters aren't equivalent." String and character comparisons in Swift aren’t locale-sensitive.

What are substrings in Swift and how to use them?

Substrings in Swift have most of the same methods as strings, which means you can work with substrings the same way you work with strings. However, unlike strings, you use substrings for only a short amount of time while performing actions on a string. When you’re ready to store the result for a longer time,...

How to get the value from a dictionary key in Swift?

To get the value using a key from a Swift Dictionary, use the following syntax. var value = myDictionary[key] The syntax is similar to accessing a value from an array using the index. Here, in case of the dictionary, key is playing the role of an index,

What is a string literal in Swift?

A string literal is a sequence of characters surrounded by double quotation marks ( " ). Use a string literal as an initial value for a constant or variable: Note that Swift infers a type of String for the someString constant because it’s initialized with a string literal value.


1 Answers

If the second parameter of scanUpToCharactersFromSet is declared as autoreleasing unsafe pointer, i.e. AutoreleasingUnsafePointer<NSString?> you should be able to invoke your function without making a pointer explicitly. Swift lets you use & operator on an NSString? variable to produce an autoreleasing unsafe pointer, like this:

var str : NSString? = nil
myScanner.scanUpToCharactersFromSet(NSCharacterSet.newlineCharacterSet(), intoString:&str)

This would give you str as a "normal" optional NSString, which you can unwrap using the regular unwrapping operators.

like image 72
Sergey Kalinichenko Avatar answered Sep 24 '22 05:09

Sergey Kalinichenko