.Type does not have a member - Error in Swift - ios7

import UIKit
class Service: NSObject {
let urlValue:NSURL = NSURL(string: "www.google.com")!
let request:NSURLRequest = NSURLRequest(URL:urlValue)
}
I am getting the error "Service.Type does not have member named urlValue".
But,
let request:NSURLRequest = NSURLRequest(URL:NSURL(string: "www.google.com")!)
Works fine!. I am confused, where I went wrong? Any help will be greatly appreciated.
Thanks!.

Related

Cannot find * in scope

I'm migrating objective C to swift. I'm getting the error Cannot find 'NSUtils' in scope in the line-
_idInstance = NSUtils.sha256(_idInstance)
I have the NSUtils class in objective C. The function is also mentioned there and it's working well with other .m files:
+ (NSString *)sha256:(NSString *)clear {
....
}
The autofill suggestions also don't show for this. Please help.
In Swift, you should really use CryptoKit instead:
import CryptoKit
let digest = SHA256.hash(data: myData)
let digestString = String(bytes: digest.map { $0 }, encoding: .ascii)!

what is the proper syntax for the following expression in swift 3?

as you can guess, this is an issue regarding swift's whole API renovation. I've read the documentation extensively but can't seem to lay my finger on a proper workaround.
I am receiving the error
Value of type 'Error' has no member 'userInfo'
on the following line:
else if let secondMessage = error?.userInfo["error"] as? String
of this block:
let query = PFQuery(className: "Images")
query.whereKey("Subject", equalTo: self.subjectName)
query.findObjectsInBackground { (objects, error) -> Void in
if error == nil {
// do something
}else if let secondMessage = error?.userInfo["error"] as? String {
// do something
}
Any suggestions? feel's like I'm missing something painstakingly obvious... dev block i guess :(

call can throw error message Swift 2

I just converted my app to Swift 2, and of course, am getting one error message:
"Call can throw, but it is not marked with 'try' and the eror is not handled"
I did search here for how to fix this, but the answers are even more confusing to me than the error itself. LOL.
My application worked perfectly until I converted it into Swift 2. Wonderful...
var myPlayer = AVAudioPlayer()
var yourSound1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("RemSound_01", ofType: "wav")!)
func initYourSound() {
myPlayer = AVAudioPlayer(contentsOfURL: yourSound1, fileTypeHint: nil)
myPlayer.prepareToPlay()
myPlayer.volume = 1.0 // < for setting initial volume, still not perfected.
}
The error throws in the following line:
myPlayer = AVAudioPlayer(contentsOfURL: yourSound1, fileTypeHint: nil)
How am I supposed to fix this? I understand that Swift is trying to "universalize" error handling, but doing so by breaking even the simplest of code seems silly to me.
Thank you for your help.
Here is the pattern you are looking for.
<throwing function> // compiler error in Swift 2
do { try <throwing function> } catch { }
In catch you usually get an error that you can handle further.
This works in Swift 2.0, put it in your viewDidLoad method ...
do {
try AudioPlayer = AVAudioPlayer(contentsOfURL: ButtonAudioURL, fileTypeHint: nil)
} catch {
print("errorin do-try-catch")
}
where ButtonAudioURL is
var ButtonAudioURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Besides", ofType: "mp3")!)

New swift syntax for initialisation of SKNode?

So today I went back to coding after a while (half a year? plus before it wasn't like I was any good lol ...) and wanted to pickup Swift but this stopped me just before I even gave it a good try:
BTW I'm trying to make a "myWorld" variable that would act as the scene handler later on, well, u get the idea......
http://postimg.org/image/4n1izokcj/
(the error can't be seen here on stackoverflow, little resolution upload...)
the old Obj-C way that worked for me for initialising a new SKNode:
#interface Level() {
SKNode* myWorld;
}
#implementation Level
-void setUpScene {
myWorld = [SKNode node]
[self addChild:myWorld]
}
the new Swift way that I thought should work:
var myWorld:SKNode?
class Level:SKScene {
func setUpScene() {
myWorld = SKNode.node()
self.addChild(myWorld!)
}
}
what am I doing wrong? (the "myWorld! = SKNode.node()" does the same...)
pls no flame.
thx for any constructive reply ^__^
OK, I'm just blind... the syntax is
myWorld = SKNode()
& not
myWorld = SKNode.node()
jeez... feel free to erase this question editors...

Obj C Init not recognized in Swift (DBCamera)

When I write the bottom Swift code I get, "Extra argument in call." Is the syntax wrong or is there some other problem? Thanks.
Objective-C Initializer:
typedef void(^CameraSettingsBlock)(DBCameraView *cameraView, id container);
- (id) initWithDelegate:(id<DBCameraViewControllerDelegate>)delegate cameraSettingsBlock:(CameraSettingsBlock)block;
Swift Initialization:
var cameraContainer = DBCameraContainerViewController(delegate: self, cameraSettingsBlock:{
(cameraView:DBCameraView, container:DBCameraContainerViewController) in
cameraView.photoLibraryButton.hidden = true
})
Got it! I had needed an ! with DBCameraView and needed to switch DBCameraContainerViewController to AnyObject!. Thanks to everyone!
var cameraContainer = DBCameraContainerViewController(delegate: self, cameraSettingsBlock:{
(cameraView:DBCameraView!, container:AnyObject!) in
cameraView.photoLibraryButton.hidden = true
})
You can probably use this code, making use of syntactic sugar to define a closure when it's the last parameter in a function:
var cameraContainer = DBCameraContainerViewController(delegate: self) { (cameraView, container) in
cameraView!.photoLibraryButton.hidden = true
}
or more concisely:
var cameraContainer = DBCameraContainerViewController(delegate: self) { $0!.photoLibraryButton.hidden = true }
I both cases type inference is used to determine types. In the latter case, the $0 is a shortcut for the first parameter passed to the closure.
Let me know if it works