var test: String
test = "Hello"
count(test)
Error message: Cannot invoke count with an argument list of type '(Int)'
test.characters.count
(Swift 2)
The error makes no sense. Your code works in Xcode 6.3.2, but gives an error in Xcode 7. It must be a Swift 2.0 issue.
If your version of Swift is before 1.2 it could be:
countElement(test)
If the version is 2.0 it is:
test.characters.count
Otherwise try:
Swift.count(test)
Related
How to a pass a nil pointer to a C API in Swift?
More specifically, I'm trying the following:
import Accelerate
let v = [1.0, 2.0]
var m = 0.0
var sd = 0.0
// 3rd arg is of type: UnsafeMutablePointer<Double>?
vDSP_normalizeD(v, 1, nil, 0, &m, &sd, vDSP_Length(v.count))
Documentation for vDSP_normalizeD is found here.
This method of passing nil seems valid for earlier versions of Swift as in this answer. However using Xcode v10.1 / Swift v4.2.1 it gives the following error message:
Nil is not compatible with expected argument type 'UnsafeMutablePointer'
The following solution does not work either:
let p: UnsafeMutablePointer<Double>? = nil
vDSP_normalizeD(v, 1, p, 0, &m, &sd, vDSP_Length(v.count))
giving the following error message:
Value of optional type 'UnsafeMutablePointer?' must be unwrapped to a value of type 'UnsafeMutablePointer'
In earlier versions of Swift the following was possible:
let p = UnsafeMutablePointer<Double>(nil)
but it now generates the following error:
Ambiguous use of 'init'
So, my question is - how do I pass a nil pointer to a C API using a modern version of Swift?
Problem solved!
Thanks OOPer for verifying that it does in fact compile and asking: Are you doing something special to your project?.
I'm maintaining my project through Swift Package Manager and apparently, when generating the Xcode project, it defaults to MACOSX_DEPLOYMENT_TARGET 10.10. However, creating a new project directly via Xcode v10.1 the default is MACOSX_DEPLOYMENT_TARGET 10.12.
The above code requires 10.11 and I did not understand that SPM didn't use a later deployment target. This is all solved now by using a Package.xcconfig file with the variable: MACOSX_DEPLOYMENT_TARGET=10.11.
The documentation didn't help from start either. The help for vDSP_normalizeD doesn't mention anything about target requirements, instead that information is found in the documentation for its float cousin vDSP_normalize.
Btw, this might be obvious for some, but it took me some hours to figure it out. Thanks again OOPer for pointing me somewhat in the right direction.
it's hard because I could not test it, but I think is because the initializer:
try this:
let p: UnsafeMutablePointer<Double>? = UnsafeMutablePointer<Double>.init(mutating: nil)
And let me know if it works for you
When i write following line of code, compiler shows error "can't assign value of type '()' to UIColor". Any explanation regarding this error ?
searchBar.layer.borderColor = UIColor.lightGrayColor().CGColor
There is no error in that syntax i tested on Swift 2.0
Try Cleaning you project and rebuilding
Go to Product
select Clean
Product>Build
And check it should work fine
sometimes XCODE parses for errors faster so it takes time to get away
I'm looking for an explanation on playground behavior for Swift. On page 76 of the book Beginning Swift Programming the doSomething function doesn't behave in Xcode as described.
func doSomething(num1: Int, num2: Int) {
println(num1, num2)
}
doSomething(5,6)
The book doesn't show an answer, but I expect a response like (5,6). However, I get no error nor any response. Change the action to println(num1) and doSomething(5,6) works. It produces 5. So does doSomething(5). For that matter. Change it to println((num1, num2)) and doSomething(5,6) yields (5,6).
I'm using Xcode v.6.4 on a Mac running Yosemite. What's going on?
As far as I know, the println() function takes only one parameter.
You either do:
println((num1, num2)) // for printing as a Tuple object
or:
println("\(num1), \(num2)") // for printing as a String object
Reference: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309
When in a normal project rather than Playground, the code you provided actually works. However, you will have to call doSomething(5, num2: 6) instead. (Swift 1.2/2.0)
It's Swift basic knowledge and suggest you to look up answer rather than ask here. If you want to print something using println with variable. You have to use \(variableName). For example:
println("\(num1), \(num2)")
I'm new to Swift and Xcode. I couldn't figure out how to take user input in Swift playground. I'm trying to write Swift equivalent of the following Python code:
def greet():
name = raw_input('What is your name?>')
print "Hello ", name
Is it possible to get user input in Swift playground?
Just use the ask() method which is available on Pad playgrounds
I am getting build error while using stricmp to compare two C strings in Xcode.
Error : Implicit declaration of stricmp invalid in C99. What is it means?
What it means is that a declaration of stricmp cannot be found in the headers you have included. Earlier versions of C allowed you to call functions that were not declared in the headers and assumed that they were declared as int function()
stricmp is not in the C or POSIX standards. For iOS look at strcasecmp() as per this SO question