I need this expression
--> sum:(abs:(amount) * -1)
and my code is:
let absExpression = NSExpression(forFunction: "abs:", arguments: [NSExpression(forKeyPath: Table.COLUMN_AMOUNT)])
let minusAbsExpression = NSExpression(format: "%# * -1", argumentArray: [absExpression])
let trueExpression = NSExpression(forFunction: "sum:", arguments: [minusAbsExpression])
but crash with down reason:
reason: 'Unsupported function expression sum:(abs:(amount) * -1)'
Related
i just need to alloc memory & set values there.Here is my code.
let radius:Float = 5.79
let sigma:Float = radius / 2
let size:Int = Int((round(radius) * 2) + 1)
var weights:UnsafeMutableRawPointer = malloc(MemoryLayout<Float>.size * size * size)
weights[some index] = some vale
but I am getting an error message in swift version 4 saying "UnsafeMutableRawPointer has no subscript members"
How can I fixed this.Any ideas please
You should better check the official documentation of UnsafeMutableRawPointer
You can write something like this:
let radius:Float = 5.79
let sigma:Float = radius / 2
let size:Int = Int((round(radius) * 2) + 1)
var weights:UnsafeMutableRawPointer = malloc(MemoryLayout<Float>.size * size * size)
weights.storeBytes(of: some value, toByteOffset: some offset, as: SomeType.self)
Or you should better use UnsafeMutablePointer<Float> instead, if all the elements are Float.
var weights:UnsafeMutablePointer<Float> = UnsafeMutablePointer.allocate(capacity: size * size)
weights[some index] = some vale
I am trying to concatenate multiple strings in swift 3:
var a:String? = "a"
var b:String? = "b"
var c:String? = "c"
var d:String? = a! + b! + c!
When compiling I get the following error:
error: cannot convert value of type 'String' to specified type 'String?'
var d:String? = a! + b! + c!
~~~~~~~~^~~~
This used to work in swift 2. I am not sure why it doesn't work anymore.
Bug report filed by OP:
SR-1122: Failure to typecheck chain of binary operators on force-unwrapped values
Which has been resolved (fix commited to master Jan 3 2017), and should hence no longer be an issue in upcoming Swift 3.1.
This seems to be a bug (not present in Swift 2.2, only 3.0) associated with the case of:
Using the forced unwrapping operator (!) for at least 3 terms in an expression (tested using at least 2 basic operators, e.g. + or -).
For some reason, given the above, Swift messes up type inference of the expression (specifically, for the x! terms themselves, in the expression).
For all the examples below, let:
let a: String? = "a"
let b: String? = "b"
let c: String? = "c"
Bug present:
// example 1
a! + b! + c!
/* error: ambiguous reference to member '+' */
// example 2
var d: String = a! + b! + c!
/* error: ambiguous reference to member '+' */
// example 3
var d: String? = a! + b! + c!
/* error: cannot convert value of type 'String'
to specified type 'String?' */
// example 4
var d: String?
d = a! + b! + c!
/* error: cannot assign value of type 'String'
to specified type 'String?' */
// example 5 (not just for type String and '+' operator)
let a: Int? = 1
let b: Int? = 2
let c: Int? = 3
var d: Int? = a! + b! + c!
/* error: cannot convert value of type 'Int'
to specified type 'Int?' */
var e: Int? = a! - b! - c! // same error
Bug not present:
/* example 1 */
var d: String? = a! + b!
/* example 2 */
let aa = a!
let bb = b!
let cc = c!
var d: String? = aa + bb + cc
var e: String = aa + bb + cc
/* example 3 */
var d: String? = String(a!) + String(b!) + String(c!)
However as this is Swift 3.0-dev, I'm uncertain if this is really a "bug", as well as what's the policy w.r.t. reporting "bugs" in a not-yet-production version of code, but possibly you should file radar for this, just in case.
As for answering your question as how to circumvent this issue:
use e.g. intermediate variables as in Bug not present: Example 2 above,
or explicitly tell Swift all terms in the 3-term expression are strings, as in Bug not present: Example 3 above,
or, better yet, use safe unwrapping of your optional, e.g. using optional binding:
var d: String? = nil
if let a = a, b = b, c = c {
d = a + b + c
} /* if any of a, b or c are 'nil', d will remain as 'nil';
otherwise, the concenation of their unwrapped values */
Swift 3
let q: String? = "Hello"
let w: String? = "World"
let r: String? = "!"
var array = [q, w, r]
print(array.flatMap { $0 }.reduce("", {$0 + $1}))
// HelloWorld!
let q: String? = "Hello"
let w: String? = nil
let r: String? = "!"
var array = [q, w, r]
print(array.flatMap { $0 }.reduce("", {$0 + $1}))
// Hello!
func getSingleValue(_ value: String?..., seperator: String = " ") -> String? {
return value.reduce("") {
($0) + seperator + ($1 ?? "")
}.trimmingCharacters(in: CharacterSet(charactersIn: seperator) )
}
let val: String? = "nil"
val.flatMap({(str: String) -> String? in
return str + "value"
})
var a:String? = "a"
var b:String? = "b"
var c:String? = "c"
var d:String? = ""
let arr = [a,b,c]
arr.compactMap { $0 }.joined(separator: " ")
compactMap be used to filter out nil values from flattened arrays
I have a code that works with print inside do block as,
do { print ([(n, 2^n) | n <- [0..19]]) }
Then i tried a much simpler version to print a variable value,
do { let a = 1; print (a) }
It throws error as parse error on input }
What else, i tried with no success ---
ghci> let a = 1; print (a)
And
ghci> :{
| let a = 1;
| print (a)
| :}
Once you start a let statement, the rest of the line is considered to be additional let assignments. Consider this error message:
ghci> do print 1; let c = 2; d = 3
<interactive>:3:13:
The last statement in a 'do' block must be an expression
let c = 2
d = 3
Note that the let keyword is not needed for d = 3.
To add a monadic statement after a let you'll need to put it on a separate line (with the correct indentation):
ghci> :{
| do print 1; let a = 2; b = 3
| print b
| :}
1
3
AFAIK, there is no way to put a monadic statement after a let on the same line.
This question already has an answer here:
cannot find an initializer for type 'String' that accepts an argument list of type '(format: String, argument: UInt32
(1 answer)
Closed 7 years ago.
let elem1 = "1"
let elem2 = "2"
let array = [elem1, elem2]
let format = "%# != %#"
//compiler error
//can't find an initializer for type...
let str = String(format: format, arguments: elem1, elem2)
//no errors but wrong output
//("%# != %#", "1", "2")
let str = String(format: format, _arguments: elem1, elem2)
//runtime error
//fatal error: can't unsafeBitCast between types of different sizes
//this is what I need
let str = String(format: format, arguments: array)
//only this works with the right output
//1 != 2
let str = String(format: format, arguments: [elem1, elem2])
print(str)
tested in xcode7 beta and xcode6.3, I couldn't find a workaround right now
Use this syntax (XCode 7):
import Foundation
let elem1 = "1"
let elem2 = "2"
let format = "%# != %#"
let str = String(format: format, elem1, elem2) // "1 !=2"
print(str) // "1 != 2\n"
The trick is to specify the overloaded ctor with format: and skip arguments: all together.
This question already has an answer here:
Trim end off of string in swift, getting error at runtime
(1 answer)
Closed 7 years ago.
Trying to get to grips with Swift programming, I wrote the following:
var s : String = "dog"
var i1 : String.Index = advance(s.startIndex, 2)
var t1 : String = s.substringToIndex(i1)
Executing this code in a playground, t1 has the value "do", as expected. However, if I try to construct an index that exceeds the string's length, this happens:
var s : String = "dog"
var i2 : String.Index = advance(s.startIndex, 4)
var t2 : String = s.substringToIndex(i2)
This time, the line var i2 ... shows the error
Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0).
I read the Swift documentation, but the entry for String.substringToIndex reads in its entirety:
func substringToIndex(index: String.Index) -> String
[Foundation]
Returns a new string containing the characters of the String up to, but not including, the one at a given index.
The result is not optional, nor does the function possess an error parameter or return an empty string in case of faulty arguments.
I don't know how to prevent this by not creating an index in the first place, because String does not have a length or count property.
Since Swift does not have exception handling, how can programs recover from errors like this?
This is on OS X 10.10.2, Xcode 6.2.
The error is in advance(s.startIndex, 4) as you cannot advance eyond the end index:
1> var s = "dog"
s: String = "dog"
2> var i1 = advance(s.startIndex, 4)
fatal error: can not increment endIndex
i1: String.Index = {
_base = { /* ... */ }
/* ... */
}
Execution interrupted. Enter Swift code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)
You avoid this by providing an end index as:
3> var i1 = advance(s.startIndex, 4, s.endIndex)
i1: String.Index = {
_base = { /* ... */ }
/* ... */
}
and then:
4> s.substringToIndex(i1)
$R0: String = "dog"
at least for Swift1.2, in Xcode6-Beta3.