Are Built-In Intrinsic Functions Available in Swift 3

Is there an intrinsic size limit for the static initializer in Swift?

It turned out that the exception came from a duplicate key, which is not allowed. If the error messages in Swift would be more user friendly that problem would have been seen much easier. ATM error messages in Swift are simply terrible.

How can I enable SSE3 in Xcode?

You just need to set the code generation option for vector instructions in your build settings:

Sample Image

In this example SSSE3 (and below) are enabled.

Note that you seem to be confused between SSE3 and SSSE3 (aka "SSE3 with supplemental instructions" or "MNI") - note the difference.

Find most significant bit in Swift

You can use the flsl() function ("find last set bit, long"):

let x = 9
let p = flsl(x)
print(p) // 4

The result is 4 because flsl() and the related functions number the bits starting at 1, the least significant bit.

On Intel platforms you can use the _bit_scan_reverse intrinsic,
in my test in a macOS application this translated to a BSR
instruction.

import _Builtin_intrinsics.intel

let x: Int32 = 9
let p = _bit_scan_reverse(x)
print(p) // 3

intrinsicContentSize() - Method does not override any method from its superclass

Please check the latest reference.
(You can easily find it just putting the word "intrinsicContentSize" in the searchbar of Apple's developer site.)

Declaration

var intrinsicContentSize: CGSize { get }

intrinsicContentSize has become a computed property, so you need to override it in this way:

override open var intrinsicContentSize: CGSize {
get {
//...
return someCGSize
}
}

Or simply:

override open var intrinsicContentSize: CGSize {
//...
return someCGSize
}

why does the argument label become irrelevant in this Swift code?

Fact: any function in Swift can be reduced to a closure, and they actually are when passed around. f and g and h can all be reduced to closures of type (String) -> String.

Next, your (some_computation)("hello") code what in fact does is to tell to the compiler that you want to create an anonymous function (aka closure), that you call with the "hello" argument.

And due to the above, (0 < 100 ? g : h)("hello") instructs the Swift compiler to transform the g and h functions to closures of type (String) -> String. And closures don't have argument labels, thus any function, with any label, matches.

However, in the problematic code snippet, the compiler can no longer uniquely identify which h "overload" to use. Thus, it bails out.

If you want to unambiguously match one of the h functions, then you need to fully qualify it:

(0 < 100 ? g : h(foo:))("hello")

The rules work the same for member functions, as long as the compiler can decide which closure to pass along, it will match the function name, without having to specify its complete name.

Main point is that in Swift you don't circulate functions, you circulate closures. There's no way to specify a variable, or a function argument, or a property, to have labeled arguments.

How does changing the intrinsicContentSize of a UITableViewCell work?

If you want to contract/expand cells based on their intrinsic content size, you need to do the following 3 steps:

  1. Configure the table view:

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44

    For static table view controller, you will need to implement heightForRowAtIndexPath and return UITableViewAutomaticDimension.

  2. Update cell constraints to reflect contracted/expanded state

  3. Ask the table view to reflect changes:

    tableView.beginUpdates()
    tableView.endUpdates()

Swift.Extensions var and func. Which one is better?

They are the same in how they work. It really is a signal of intent. Personally I would recommend the function in this case, which is slightly counter-intuitive, so I'll explain.

The first rule is "does it have side effects?" If so, it should be a function.

The second rule is "is it O(1)?" (That means "it takes a constant, and generally assumed to be short, period of time to run." In other words, is it "cheap?") If not, it should be a function.

But the third, more subtle rule is "is it reasonably considered a "property" of the instance?" And in this case I'd argue, no. It is a completely different thing than the instance. It is something computed on the instance, not something that is an intrinsic part of the instance. It would be fairly ridiculous for this to be replaced with a non-computed property (you'd never store "4" as the "abs" field of "-4"). So I would make it a function.

Note that in Swift 3, abs is a static function on the type (for example, it's Double.abs(4.0) rather than (4.0).abs). That doesn't invalidate your question, but given this specific case, that's how the team chose to address it, and I think it's a better way.

native zlib inflate/deflate for swift3 on iOS

I just recently had to add that exact library and file to my project, and after a lot of troubleshooting finally got it working, so let me walk you through the steps!

Okay

1) Go to the top level directory of your project in finder, and create a new folder called Swiftzlib or whatever you want the name of the module that you will be importing to be. (What we will do is add the zlib library as a module, so think of it as importing Foundation or some such other module). To clarify, this Swiftzlib directory will end up as a child directory of the same directory that contains your *.xcodeproj and *.xcworkspace files.

2) Inside the folder you created, make two files.

  • include.h
  • module.modulemap

3) In your include.h file, enter the following:

#include<zlib.h>

4) In your module.modulemap file, enter the following:

module Swiftzlib [system] {
header "include.h"
export *
}

Where Swiftzlib is the same as the name of the folder that you created.

5) Open your Xcode project, and select your target

  • 5a) In Build Phases -> Link Binary with Libraries, add libz.tbd
  • 5b) In Build Settings -> Swift Compiler - Search Paths, add $(PROJECT_DIR)/Swiftzlib non-recursively to the import paths
  • 5c) In Build Settings -> Other Linker Flags, add -lz as a flag

6) Select your project in Xcode (may not be necessary, but I've done it in my project and it works)

  • 6a) In Build Settings -> Swift Compiler - Search Paths, add $(PROJECT_DIR)/Swiftzlib non-recursively to the import paths

7) In Data+Gzip.swfit, add import Swiftzlib to the top of the file

8) Clean, Build, and Run!



Related Topics



Leave a reply



Submit