Use '#selector' instead of explicitly constructing a 'Selector' warning - ios

I have this line in my code:
let argsort = (scores as NSDictionary?)?.keysSortedByValue(using: Selector("compare:"))
but XCode gives me a Use '#selector' instead of explicitly constructing a 'Selector' warning. However, the proposed fix:
let argsort = (scores as NSDictionary?)?.keysSortedByValue(using: #selector(NSTextLocation.compare(_:)))
requires iOS version 15.0, and therefore does not work for me (I am targetting iOS 13.0).
How can I either silence this warning or find a way out of this? Thanks

Related

Swift and Objc interoperability - availability not working only in objc

While here Apple states that the Swift available flag should be applied also in objc, it's not working for me. What am I doing wrong?
I've got following declarations in Swift files:
#objc protocol Readable: AnyObject {...}
#available(iOS 10.3, *)
#objc class Reader: NSObject, Readable {...}
So let's check if it produces an error when I try to initialize it in pre-ios-10 project without version check. If I write following code in Swift:
let tmp = Reader()
it returns an error:
'Reader' is only available on iOS 10.3 or newer
What is expected.
However if I write following code in objc:
// if (#available(iOS 10.3, *)) { // commeted out to test if it succeeds without version check
Reader *tmp = [[Reader alloc] init];
// }
The build is finished without any error, while I'd expect the same error as in Swift.
I've tried to mark the class with:
#available(iOS 11, *)
#available(iOS, introduced: 10.3)
Neither of these works (produces an error) in objc. Any help, please?
Objective-C has had __attribute__((availability)) for longer than it has had #available. To make it work, the Objective-C compiler weakly links symbols that are not available on the deployment target. This means that compiling always succeeds, and starting your app succeeds, but the symbol will be NULL at runtime if it is not available.
Depending on what it is, you'll get more or less graceful degradation when you try to use it:
calling a weakly-linked function that is missing is going to crash
reading or writing to a global variable that is missing is going to crash
using a class that is missing will be a no-op and all methods are going to return zero
The old way of testing whether a symbol is found at runtime is just to compare it to NULL:
NS_AVAILABLE_MAC(...) #interface Foo #end
int bar NS_AVAILABLE_MAC(...);
int baz(int frob) NS_AVAILABLE_MAC(...);
if ([Foo class]) { /* Foo is available */ }
if (&bar) { /* bar is available */ }
if (&baz) { /* baz is available */ }
In your case:
Reader *tmp = [[Reader alloc] init];
tmp will be nil, because that would be the same as [[nil alloc] init].
The #available directive has only relatively recently been added to Objective-C. It's now possible to use #available in Objective-C in the same way that you use #available in Swift. However, to preserve backwards compatibility, it possibly never will be a compile-time error (at default error levels) to try to use a symbol that might not be available on the deployment target in Objective-C.

os_log - use of unresolved identifier error

I'm trying to use the new os_log API, by logging a simple statement:
os_log("Hello")
And I get an error for os_log:
Use of unresolved identifier 'os_log'
I also tried wrapping it in a block, like this
if #available(iOS 10.0, *) {
let foo: StaticString = "Something happened."
os_log(foo)
}
And I still get the same error. I would like to use this in Swift 4.
I looked for possible frameworks that might be required, and found no likely candidates.
I found no solution from these links either:
https://developer.apple.com/documentation/os/logging
https://developer.apple.com/videos/play/wwdc2016/721/
Because you forgot to
import os
at the start of this file.

How to implement firebaseUI in IOS

I new to IOS Development.I am basically android developer
I want to implement firebaseUI for login using mobile number and email
I got doc here https://firebase.google.com/docs/auth/ios/firebaseui?authuser=0. But i am getting compiletime error..below I mention
My AppDelegate file
my ViewController class
if I comment this line authUI.delegate = self above line getting wrong below I add image...
authUI is a Swift optional, which means it can be nil (null). In order to use optionals, you must unwrap them with the '?' sign. So your code can be simply fixed with:
authUI?.delegate = self
If authUI is nil, the code will do nothing, whereas in Java it would cause a null pointer exception. You may unwrap it with '!' instead, which will cause a runtime error if authUI is nil.
Tip: you can click on the red error or yellow warning icons to see and apply suggested fixes, much like Alt-Enter on Android Studio. Do not rely too much on those, sometimes the automatic fixes become a mess. :)

'init()' is deprecated: init() will be removed in Swift 3. Use `nil`

I was using this code.
var audioUnit:AudioUnit = AudioUnit()
But in Xcode 7.3 (Swift 2.2) I am getting this warning. Any idea why? And how can I get rid of that?
N.B. Is I use nil then my program crashes.
AudioUnit is a typealias of AudioComponentInstance which itself is a typealias of COpaquePointer. Initializing pointers will be removed with Swift 3.0, just set the variable to nil.
nil and AudioUnit() should do the same thing. If your program is crashing with nil, you probably have a bug somewhere else.

EXC_BAD_ACCESS when updating Swift dictionary after using it for evaluate NSExpression

I'm using a dictionary to evaluate an expression, when the expression has variables and the dictionary is actually used by NSExpression, something happens and I get EXC_BAD_ACCESS when trying to update the dictionary, this only happens when debugging in an iPhone6, not in the simulator and not in an iPhone 4S.
let strExpression = "a+b+20"
let exp = NSExpression(format:strExpression)
self.dictionary = ["a":10.0, "b":15.0, "c":25.0]
let value:AnyObject = exp.expressionValueWithObject(self.dictionary, context: nil)
let doubleValue = value as Double
self.dictionary.updateValue(doubleValue, forKey: "c")
Something really weird is that if i add this line just after creating the dictionary, then it woks fine:
let newDic = self.dictionary
I,m using iOS 8.1. Thanks in advance!
With #bensarz comment, I thought it might be helpful for others searching for answers if I put the response into an actual answer instead of a comment.
Per #LeeWhitney's response on a similar post:
Looks like a compiler bug.
Have you tried switching between Release and Debug then rebuilding? If debug works but not release it can be an indication of a compiler/optimizer bug.
Does it happen in the simulator also?
Your code works for me on iOS 8.1 with XCode 6.1.
Solution:
The issue seems to be solved by changing the 'Optimization Level' under the 'Swift Compiler - Code Generation' to 'None'. The issue seems to be with the 'Fastest' Compiler optimization level.
Also, a work around that I've found original before the compiler change:
If you use a let statement prior to assigning values in the dictionary, it seems to alleviate the issue. More information found at link below:
EXC_BAD_ACCESS on iOS 8.1 with Dictionary

Resources