Swift Guard-Let Statement snippet - swift

Why autocomplete for guardlet snippet doesn't work in closures? For example:
DispatchQueue.main.async(execute: {
guardlet //no any suggestions by Xcode
})
Shouldn't I use this control statement in any closure? Or it's just wrong scopes in the snippet?
I'm using Xcode 8.2 (8C38).

It works in Xcode 8.2.1:
Make sure you do not have pending compilation errors in your project.
You can close Xcode and reopen it eventually.

DispatchQueue.main.async(execute: {
guard let x = x where x > 0 else {
// Value requirements not met, do something
}
})
N.B:- Hi,there are no such key available "guardlet" in xcode please write that code"guard let"

Related

Using previewCGImageRepresentation in both Xcode 13 and Xcode 12?

I’ve run into an issue where Xcode 13b2 (iOS 15 SDK) changed the Swift return type of AVCapturePhoto.previewCGImageRepresentation(). In Xcode 12.5.1 (iOS 14 SDK), this method returns Unmanged<CGImage>. In 13b2 - 13b4, it returns CGImage?.
I need my code to compile under both Xcode versions, since Xcode 13 has other issues, and can’t be used to submit builds to the App Store. I thought I was clever writing this, but it won’t compile, because it’s not conditional code compilation check but rather a runtime check:
extension AVCapturePhoto {
func stupidOSChangePreviewCGImageRepresentation() -> CGImage? {
if #available(iOS 15, *) {
return self.previewCGImageRepresentation()
} else {
return self.previewCGImageRepresentation()?.takeUnretainedValue()
}
}
}
Another possibility might be to create a user-defined Xcode setting, but I don’t think that can be done conditionally based on Xcode or SDK version.
There might be some unsafe pointer histrionics one can do…
Any other ideas?
You can make a check on the swift compiler version. An exhaustive list is available on wikipedia at the time of writing.
https://en.wikipedia.org/wiki/Xcode#Toolchain_versions
extension AVCapturePhoto {
func stupidOSChangePreviewCGImageRepresentation() -> CGImage? {
#if compiler(>=5.5)
return self.previewCGImageRepresentation()
#else
return self.previewCGImageRepresentation()?.takeUnretainedValue()
#endif
}
}

How to stop ´enumerateChildNodesWithName("//*") ´ enumeration?

How do I stop this enumeration? I have the following code and Xcode complaining that I cannot assign a value to let constant. Stopping is probably a simple thing but I'm quite the noobie with Swift, so please bear with me.
self.enumerateChildNodesWithName("//*") {
spaceshipNode, stop in
if (( spaceshipNode.name?.hasSuffix("ship") ) != nil) {
for enemyNode in self.children {
if (enemyNode.name == "enemy"){
if(enemyNode.containsPoint(spaceshipNode.position)){
self.gotoGameOverScene(spaceshipNode)
stop = true // Error: Cannot assign to value: 'stop' is a 'let' constant
}
}
}
}
}
Generally you'd better show your code as text. With which we can easily copy & paste, and test it, or edit it.
In your code, the type of stop should be shown as UnsafeMutablePointer<ObjCBool> in the Quick Help pane of Xcode.
You need to modify the content of the pointer, in Swift2:
stop.memory = true
In Swift 3, the property memory is renamed to pointee:
stop.pointee = true
Apple's documentation suggests that we stop enumeration via stop.initialize(to: true).

How can I clear the swift warning "will never be excuted"?

Now I am learning the swift and when I use the if - else ,the Xcode shows me a warniing "will not be excuted".Though it isn't a big problem, I don't want to see this, how can I clear this warning in the project?
This is from a logic error that the compiler has picked up and is warning you about.
It gives the line number in your code that can never be reached.
a) Change the logic of your code
b) delete or comment out lines of code that can never be reached
The compiler will not give this message unnecessarily
example
if 1 == 2 {
a = 3
}
else {
a = 4
}
Obviously the condition is never met, the a=3 assignment can never happen.
let a = 3;
let b = 4;
if (a == 3) {
print("executed")
} else if (a == 5) {
print("never be executed")
} else {
print("not executed")
}
Apple's LLVM compiler is pretty verbose when it tells you about warnings - if you check the logs you would find something like:
warning: will never be executed [-Wunreachable-code]
So to suppres that warning, you need to set the flag -Wno-unreachable-code. This works for the ObjC compiler, but currently isn't supported by the Swift compiler

Why is this guard statement throwing me an error?

So I'm following a tutorial form Lynda.com for making a iOS app with Swift and when I plug this line of code in, it's throwing me errors:
guard let text:String = addressBar.text else
The error I get is:
Consecutive statements on line must be separated by ';'
Once I have Xcode fix it, these are the errors I get:
Expected expression.
Use of unresolved identifier 'guard'.
Expression resolves to an unused function.
Braced block of statements is an unused closure.
I'm really new to Xcode and Swift so any help would be awesome! Thanks!
Because you using outdated xcode and swift language. Latest version is xcode 7 and swift 2.
https://developer.apple.com/xcode/
May be you are using a wrong version of Xcode(version 7.0)
Try it too:
Be certain you are using guard statement in the right conditions. E.g:
class AddressBar {
var text: String? = ""
}
var addressBar = AddressBar()
addressBar.text = nil
//addressBar.text = "text"
func test() {
guard let _text: String = addressBar.text else {
print("Nothing")
return
}
print("I reach this point")
}
test()

SDWebImage and Swift

I've succeeded in incorporating SDWebImage (written in Objective-C) with my Swift project - but its still acting a bit funny. Specifically, its giving me an error in the if statement inside the following Closure:
let completionBlock: SDWebImageCompletionBlock! = {
(image:UIImage!, error: NSError!, cacheType:SDImageCacheType, imageURL:NSURL!) -> Void in
if (image && cacheType == SDImageCacheType.SDImageCacheTypeNone) {
cell.productImageView.alpha = 0.0
UIView.animateWithDuration(1.5, animations: {
cell.productImageView.alpha = 1.0
})
}
}
cell.productImageView.sd_setImageWithURL(imageURL!, placeholderImage:UIImage(named:"Icon120pix.png"), completed: completionBlock)
The error I'm getting on that if statement is: Use of unresolved identifier 'SDImageCacheTypeNone'
This makes no sense because SDImageCacheTypeNone is one of the values defined in the SDImageCacheType typedef.
By the way, if I take that if statement out and leave just the statements inside it, everything works just fine.
So any ideas what I might be doing wrong here?
Have a look at this document in the Enumerations section for a more in depth explanation as to why Swift displays Objective-C enums differently.
Apple Swift Documents
In your code example the way to solve the problem is to use:
SDImageCacheType.None
Instead of
SDImageCacheType.SDImageCacheTypeNone