What does StringInterpolationSegment do? (swift) - swift

I continued on my math question project, and it could now generate questions, so I need to try to use a global variable to use my answer's in generating the answer itself and putting the answer randomly into one of 4 choices. I decided to put a secret label outside of my view that shows the answer. Here's my code:
//important stuff
#IBOutlet var secretAnsarrrrr: UILabel!
//useless code
//declare the value of secretAnsarrrr
secretAnsarrrrr.text = String(answer)
numA.text = String(Int(randomNumber))
numB.text = String(Int(randomNumber2))
}
generateQuestion()
}
var optionAnswer:UInt32 = arc4random_uniform(4)
#IBAction func answerA(sender: UIButton) {
var otherAns:String = String(secretAnsarrrrr) //error
if optionAnswer == 0 {
optionA.text = secretAnsarrrrr.text
}
Doing so gives me the error of 'Missing argument label: StringInterpolationSegment in call'. It tells me to place it behind 'secretAnsarrrrr' in the parentheses. What does stringInterpolationSegment do and how do I fix this?

secretAnsarrrrr is not a string itself - it is a UILabel which also contains information like color and font. You need to tell Swift to use its text property:
var otherAns:String = String(secretAnsarrrrr.text)

I just encountered this error, and after some head scratching, I realised that I needed to unwrap an optional integer which I was trying to parse with String(...). As soon as I added the exclamation mark, the problem was solved:
var num: Int? = 1
var str = String(num!)

it is Better to write:
var num: Int? = 1
var str = num.map { String($0) } // if num == nil then str = nil else str = string representation

Related

Swift NSTextField stringValue not displaying set value

Hello Sultans of Swift!
I am new to Swift although I have used C, C++ and C# a lot. I have come upon a situation that is really puzzling me. I'm going to post a code snippet here:
#IBAction func myFunc(sender: AnyObject)
{
let dirPicker: NSOpenPanel = NSOpenPanel()
dirPicker.allowsMultipleSelection = false
dirPicker.canChooseFiles = true
dirPicker.canChooseDirectories = false
dirPicker.runModal()
let selection = dirPicker.URL
if(selection != nil)
{
do
{
print(selection)
let mp3File = try MP3File(path: (selection?.path)!)
let title = mp3File.getTitle()
// This prints OK
print("Title:\t\(mp3File.getTitle())")
// This prints OK too
print("Title:\t\(title)")
print("Artist:\t\(mp3File.getArtist())")
print("Album:\t\(mp3File.getAlbum())")
print("Lyrics:\n\(mp3File.getLyrics())")
fileName.stringValue = (selection?.path)!
// This sets the label songTitle to an empty space and I can't see why.
// If I initialise title to:
// let title = "STRING CONSTANT"
// ...instead of
// let title = mp3File.getTitle()
// ...then it does actually set the correct text on the label songTitle.
// In both cases, printing to the console works fine! Its just setting
// the text on the label that is eluding me!
songTitle.stringValue = title
}
catch ID3EditErrors.FileDoesNotExist
{
print("Error: File Does Not Exist")
}
catch ID3EditErrors.NotAnMP3
{
print("Error: Not an MP3")
}
catch let e
{
print(e)
}
}
}
When I try to set the text in a label by setting its stringValue property to a variable it just displays empty space, yet I can actually print the variable to the console just fine. The variable is set as the return value of a function. Now if I instead set the variable explicitly to a string constant then it works. So this will be perhaps related to the uncertainty of the return value of the function, but I know it contains the text because I can print it to the console.
Can anyone spot what on earth is happening here?
Thanks
EDIT: I just fixed the comments in the code to refer to songTitle instead of fileName - sorry for the confusion. This is about setting songTitle.stringValue = title
EDIT: This is the definition of songTitle:
#IBOutlet weak var fileName: NSTextField!
#IBOutlet weak var songTitle: NSTextField!
Note that setting the stringValue property of these does actually work so long as I am not using the a variable that is assigned the return value of mp3File.getTitle(). Note also that mp3File.getTitle() does return a value and I can print it to the console OK.
When you have a String value which is printed fine:
print(title) //->I Ran
But cannot be processed well:
songTitle.stringValue = title //->`songTitle` shows nothing!!!
(Of course, you have confirmed that songTitle is fine by assigning constant string to it.)
One possible reason may be existing some control characters in the String.
You can use debugPrint to reveal such cases:
debugPrint(title) //->"\0I Ran\0"
debugPrint uses String literal-like format to show control characters.
In this case, you have NUL characters (U+0000) at both ends.
So, one quick fix is trimming them at each time you get such strings:
Swift 2:
let title = mp3File.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
Swift 3:
let title = mp3File.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
Or you can write an extension, if you cannot touch the original source of the library:
Swift 2:
extension MP3File {
var title: String {
return self.getTitle().stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\0"))
}
}
Swift 3:
extension MP3File {
var title: String {
return self.getTitle().trimmingCharacters(in: CharacterSet(charactersIn: "\0"))
}
}
(Assuming MP3File does not have a property named title.)
and use it as:
let title = mp3File.title

Get a Swift Variable's Actual Name as String

So I am trying to get the Actual Variable Name as String in Swift, but have not found a way to do so... or maybe I am looking at this problem and solution in a bad angle.
So this is basically what I want to do:
var appId: String? = nil
//This is true, since appId is actually the name of the var appId
if( appId.getVarName = "appId"){
appId = "CommandoFurball"
}
Unfortunately I have not been able to find in apple docs anything that is close to this but this:
varobj.self or reflect(var).summary
however, this gives information of what is inside the variable itself or the type of the variable in this case being String and I want the Actual name of the Variable.
This is officially supported in Swift 3 using #keyPath()
https://github.com/apple/swift-evolution/blob/master/proposals/0062-objc-keypaths.md
Example usage would look like:
NSPredicate(format: "%K == %#", #keyPath(Person.firstName), "Wendy")
In Swift 4 we have something even better: \KeyPath notation
https://github.com/apple/swift-evolution/blob/master/proposals/0161-key-paths.md
NSPredicate(format: "%K == %#", \Person.mother.firstName, "Wendy")
// or
let keyPath = \Person.mother.firstName
NSPredicate(format: "%K == %#", keyPath, "Andrew")
The shorthand is a welcome addition, and being able to reference keypaths from a variable is extremely powerful
As per the updated from this answer, it is supported in Swift 3 via #keyPath
NSPredicate(format: "%K == %#", #keyPath(Person.firstName), "Andrew")
This is my solution
class Test {
var name: String = "Ido"
var lastName: String = "Cohen"
}
let t = Test()
let mirror = Mirror(reflecting: t)
for child in mirror.children {
print(child.label ?? "")
}
print will be
name
lastName
This works:
struct s {
var x:Int = 1
var y:Int = 2
var z:Int = 3
}
var xyz = s()
let m = Mirror(reflecting: xyz)
print(m.description)
print(m.children.count)
for p in m.children {
print(p.label as Any)
}
I've come up with a swift solution, however unfortunately it doesn't work with Ints, Floats, and Doubles I believe.
func propertyNameFor(inout item : AnyObject) -> String{
let listMemAdd = unsafeAddressOf(item)
let propertyName = Mirror(reflecting: self).children.filter { (child: (label: String?, value: Any)) -> Bool in
if let value = child.value as? AnyObject {
return listMemAdd == unsafeAddressOf(value)
}
return false
}.flatMap {
return $0.label!
}.first ?? ""
return propertyName
}
var mutableObject : AnyObject = object
let propertyName = MyClass().propertyNameFor(&mutableObject)
It compares memory addresses for an object's properties and sees if any match.
The reason it doesn't work for Ints, Floats, and Doubles because they're not of type anyobject, although you can pass them as anyobject, when you do so they get converted to NSNumbers. therefore the memory address changes. they talk about it here.
For my app, it didn't hinder me at all because I only needed it for custom classes. So maybe someone will find this useful. If anyone can make this work with the other datatypes then that would be pretty cool.
Completing the accepted answer for extensions:
The property needs to be #objc.
var appId: String? {
....
}
You need to use #keyPath syntax, \ notation is not supported yet for extensions.
#keyPath(YourClass.appId)
The best solution is Here
From given link
import Foundation
extension NSObject {
//
// Retrieves an array of property names found on the current object
// using Objective-C runtime functions for introspection:
// https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html
//
func propertyNames() -> Array<String> {
var results: Array<String> = [];
// retrieve the properties via the class_copyPropertyList function
var count: UInt32 = 0;
var myClass: AnyClass = self.classForCoder;
var properties = class_copyPropertyList(myClass, &count);
// iterate each objc_property_t struct
for var i: UInt32 = 0; i < count; i++ {
var property = properties[Int(i)];
// retrieve the property name by calling property_getName function
var cname = property_getName(property);
// covert the c string into a Swift string
var name = String.fromCString(cname);
results.append(name!);
}
// release objc_property_t structs
free(properties);
return results;
}
}

(swift) Error: can not invoke '>' with an argument list of type '(UInt32, #lvalue UInt32)'

class ViewController: UIViewController {
#IBOutlet weak var inputField: UITextField!
#IBOutlet weak var output: UITextView!
var guesses : UInt = 0
var number : UInt32 = 0
var gameOver = false
let MAX_GUESSES : UInt = 8
#IBAction func guess(sender: UIButton) {
var possibleGuess : Int? = inputField.text.toInt()
if let guess = possibleGuess {
// possibleGuess exists!
} else {
consoleOut("Please input a valid number!\n")
clearInput()
}
if UInt32(guess) > Int(number) {
consoleOut("\(guess): You guessed too high!\n")
++guesses
} else if UInt32(guess) < number {
consoleOut("\(guess): You guessed too low!\n")
++guesses
} else {
consoleOut("\n\(guess): You win!\n")
consoleOut("Go again? (Y)")
guesses = 0
gameOver = true
}
clearInput()
if (guesses == MAX_GUESSES) {
consoleOut("\nYou lose :(\n")
consoleOut("Go again? (Y)")
guesses = 0
gameOver = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
number = generateNewNumber()
consoleOut("Gondolkodom egy számot...\n")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func consoleOut(text : String) {
output.text = output.text + text
}
func generateNewNumber () -> UInt32 {
return arc4random_uniform(100)
}
func clearInput() {
inputField.text = ""
}
}
This is the code that I use and I get the error message at if UInt32(guess) > Int(number) {. I really can't get through this.
(swift) Error: can not invoke '>' with an argument list of type '(UInt32, #lvalue UInt32)'
* This is not exactly your problem, but it may show you a way to get around it :) *
This must be a Swift bug like many others ObjectiveC had.
I'm having the same problem trying to compare an arc4random() number (which is an UInt32 type) with a UInt32() casted string, and I get the same error, which is more outrageous in my case because the two numbers ARE the same type. Which leads me to think that the casting must not be producing the desired result.
I though of creating an auxiliary UIint32 variable and assign it UInt32(theString), butSwift doesn't let you convert a String into UInt32 when defining a variable, so I had to create an auxiliary variable to be converted to Int, and then convert the Int to UInt32 to be able to compare the two numbers:
var theString = "5"
var randomNumber = arc4random() % 10
var UInt32Number = UInt32(theString)
// => ERROR: "Cannot invoke 'init' with an argument of type '#lvalue String!'
// (this is where I realized the comparison line could be suffering from the same problem)
if randomNumber == UInt32(theString) { ... }
// No error here 'cos Swift is supposed to have casted theString into a UInt32
// ...but surprisingly it prompts an ERROR saying it can't compare a UInt32 with a UInt32 (WTF!)
// And here's where I go crazy, because watch what happens in the next lines:
var intValue = theString.toInt()
var UInt32Value = UInt32(intValue!)
if randomNumber == UInt32Value { ... } // => NOW IT WORKS!!
CONCLUSION: Swift is not making the conversion type in the comparison even if it's supposed to. Sometimes it seems to f*** up. Using auxiliary variables with set types can get around the problem.

How to append a character to a string in Swift?

This used to work in Xcode 6: Beta 5. Now I'm getting a compilation error in Beta 6.
for aCharacter: Character in aString {
var str: String = ""
var newStr: String = str.append(aCharacter) // ERROR
...
}
Error: Cannot invoke append with an argument of type Character
Update for the moving target that is Swift:
Swift no longer has a + operator that can take a String and an array of characters. (There is a string method appendContentsOf() that can be used for this purpose).
The best way of doing this now is Martin R’s answer in a comment below:
var newStr:String = str + String(aCharacter)
Original answer:
This changed in Beta 6. Check the release notes.I'm still downloading it, but try using:
var newStr:String = str + [aCharacter]
This also works
var newStr:String = str + String(aCharacter)
append append(c: Character) IS the right method but your code has two other problems.
The first is that to iterate over the characters of a string you must access the String.characters property.
The second is that the append method doesn't return anything so you should remove the newStr.
The code then looks like this:
for aCharacter : Character in aString.characters {
var str:String = ""
str.append(aCharacter)
// ... do other stuff
}
Another possible option is
var s: String = ""
var c: Character = "c"
s += "\(c)"
According to Swift 4 Documentation ,
You can append a Character value to a String variable with the String type’s append() method:
var welcome = "hello there"
let exclamationMark: Character = "!"
welcome.append(exclamationMark)
// welcome now equals "hello there!"
var stringName: String = "samontro"
var characterNameLast: Character = "n"
stringName += String(characterNameLast) // You get your name "samontron"
I had to get initials from first and last names, and join them together. Using bits and pieces of the above answers, this worked for me:
var initial: String = ""
if !givenName.isEmpty {
let char = (givenName as NSString).substring(with: NSMakeRange(0, 1))
let str = String(char)
initial.append(str)
}
if !familyName.isEmpty {
let char = (familyName as NSString).substring(with: NSMakeRange(0, 1))
let str = String(char)
initial.append(str)
}
for those looking for swift 5, you can do interpolation.
var content = "some random string"
content = "\(content)!!"
print(content) // Output: some random string!!
let original:String = "Hello"
var firstCha = original[original.startIndex...original.startIndex]
var str = "123456789"
let x = (str as NSString).substringWithRange(NSMakeRange(0, 4))
var appendString1 = "\(firstCha)\(x)" as String!
// final name
var namestr = "yogesh"
var appendString2 = "\(namestr) (\(appendString1))" as String!*

Working with optionals in Swift programming language

As far as I know the recommended way to use optionals (Int in this example) is the following:
var one:Int?
if var maybe = one {
println(maybe)
}
Is it possible to use a shorter way to do something like the following?
var one:Int?
var two:Int?
var three:Int?
var result1 = one + two + three // error because not using !
var result2 = one! + two! + three! // error because they all are nil
Update
To be more clear about what I'm trying to do: I have the following optionals
var one:Int?
var two:Int?
var three:Int?
I don't know if either one or two or three are nil or not. If they are nil, I wan't them to be ignored in the addition. If they have a value, I wan't them to be added.
If I have to use the recommended way I know, it would look something like this: (unnested)
var result = 0
if var maybe = one {
result += maybe
}
if var maybe = two {
result += maybe
}
if var maybe = three {
result += maybe
}
Is there a shorter way to do this?
Quick note - if let is preferred for optional binding - let should always be used where possible.
Perhaps Optionals aren't a good choice for this situation. Why not make them standard Ints with a default value of 0? Then any manipulation becomes trivial and you can worry about handling None values at the point of assignment, rather than when you're working on the values?
However, if you really want to do this then a tidier option is to put the Optionals into an Array and use reduce on it:
let sum = [one,two,three,four,five].reduce(0) {
if ($1) {
return $0 + $1!
}
return $0
}
That's exactly the point of optionals — they may be nil or non-nil, but unwrapping them when they're nil is an error. There are two types of optionals:
T? or Optional<T>
var maybeOne: Int?
// ...
// Check if you're not sure
if let one = maybeOne {
// maybeOne was not nil, now it's unwrapped
println(5 + one)
}
// Explicitly unwrap if you know it's not nil
println(5 + one!)
T! or ImplicitlyUnwrappedOptional<T>
var hopefullyOne: Int!
// ...
// Check if you're not sure
if hopefullyOne {
// hopefullyOne was not nil
println(5 + hopefullyOne)
}
// Just use it if you know it's not nil (implicitly unwrapped)
println(5 + hopefullyOne)
If you need to check multiple optionals at once here there are a few things you might try:
if maybeOne && maybeTwo {
println(maybeOne! + maybeTwo!)
}
if hopefullyOne && hopefullyTwo {
println(hopefullyOne + hopefullyTwo)
}
let opts = [maybeOne, maybeTwo]
var total = 0
for opt in opts {
if opt { total += opt! }
}
(It seems you can't use the let optional binding syntax with more than one optional at once, at least for now...)
Or for extra fun, something more generic and Swifty:
// Remove the nils from a sequence of Optionals
func sift<T, S: Sequence where S.GeneratorType.Element == Optional<T>>(xs: S) -> GeneratorOf<T> {
var gen = xs.generate()
return GeneratorOf<T> {
var next: T??
do {
next = gen.next()
if !next { return nil } // Stop at the end of the original sequence
} while !(next!) // Skip to the next non-nil value
return next!
}
}
let opts: [Int?] = [1, 3, nil, 4, 7]
reduce(sift(opts), 0) { $0 + $1 } // 1+3+4+7 = 15