Sound ID failing in Swift - xcode

I have the following code linked up to a button that should play some audio.
var ExamplePlayer = AVAudioPlayer()
var Example = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Example", ofType: "mp3")!)
func ExampleAudio(){
ExamplePlayer = AVAudioPlayer(contentsOfURL: Example, error: nil)
ExamplePlayer.prepareToPlay()
ExamplePlayer.play()
}
#IBAction func anotherExampleAudio(sender: AnyObject) {
ExampleAudio()
}
This is the error I'm getting.
fatal error: unexpectedly found nil while unwrapping an Optional value
What's going on?

Related

to look at the suffix of the text of the text field

I want to let the people login to the certain domain, but it always shows a error.
Cannot convert value of type 'String' to expected argument type 'Int').
how can I deal with it?
import UIKit
import FirebaseAuth
class ViewController: UIViewController {
#IBOutlet var emailTextField: UITextField!
#IBOutlet var passwordTextField: UITextField!
#IBAction func signUp(_ sender: Any) {
Auth.auth().createUser(withEmail: emailTextField.text!, password: passwordTextField.text!) { (result, error) in
if error != nil{
print(error)
}else{
self.performSegue(withIdentifier: "createdUser", sender: self)
}
}
}
#IBAction func signIn(_ sender: Any) {
Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!) { (result, error) in
if error != nil{
print(error)
}else{
var hi = self.emailTextField.text
if hi?.suffix("#hkugac.edu.hk"){ //error here
self.performSegue(withIdentifier: "logged", sender: self)
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Try using the hasSuffix method. https://developer.apple.com/documentation/swift/string/1541149-hassuffix
var hi = self.emailTextField.text
if let mail = hi, mail.haSuffix("#hkugac.edu.hk")
{
self.performSegue(withIdentifier: "logged", sender: self)
}
else
{
// Dome some error handling here.
}
Also, I think it would be better to check the mail before you call the signIn function.

Using SSL from Swift

I'm trying to connect to IRC via SSL using CocoaAsyncSocket and I seem to be going wrong somewhere. Here's the code I'm working with:
func identity(named name: String, password: String) throws -> SecIdentity {
let url = Bundle.main.url(forResource: name, withExtension: "p12")!
let data = try Data(contentsOf: url)
var importResult: CFArray? = nil
let err = SecPKCS12Import(
data as NSData,
[kSecImportExportPassphrase as String: password] as NSDictionary,
&importResult
)
guard err == errSecSuccess else {
throw NSError(domain: NSOSStatusErrorDomain, code: Int(err), userInfo: nil)
}
let identityDictionaries = importResult as! [[String:Any]]
return identityDictionaries[0][kSecImportItemIdentity as String] as! SecIdentity
}
public func socket(_ sock: GCDAsyncSocket, didConnectToHost host: String, port: UInt16) {
let sslSettings = NSMutableDictionary()
sslSettings[kCFStreamSSLCertificates] = try! identity(named: "ssl", password: "")
sslSettings.addEntries(from: [kCFStreamSSLLevel: StreamSocketSecurityLevel.negotiatedSSL, kCFStreamSSLPeerName: host, kCFStreamSSLValidatesCertificateChain: false])
socket?.startTLS(sslSettings as! [String : NSObject])
}
I'm just getting a "Socket closed by remote peer" error. Could there be an issue with the cert I've generated? Can't seem to find any information anywhere about how to generate a proper cert.

Xcode8 Swift3 ImageURL returns nil

I am trying to download an image from my web server, using Xcode 8 and Swift 3. It looks like this. The request returns nil. Any suggestions why?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var view1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func load(_ sender: UIButton) {
let url = URL(string:"http://www.example.com/folder2/pic3-1.jpg")
let task = URLSession.shared.dataTask(with: url!) { data, response,
error in
guard let data = data, error == nil else { return }
DispatchQueue.main.sync() {
self.view1.image = UIImage(data: data)
}
}
task.resume()
}
#IBAction func reset(_ sender: UIButton) {
self.view1.image="blankU.png"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Any idea why the image is returning nil from the server?
The Xcode8, Swift3 coding is okay. The server is blocked.

itemForActivityType not being called

i just made one wrapper class for share helper my code is as follow.
let activityVC = UIActivityViewController(activityItems: [message], applicationActivities: nil)
activityVC.setValue(subject, forKey: "subject")
activityVC.completionWithItemsHandler = {(activityType: String!, completed:Bool, objects:[AnyObject]!, error:NSError!) in
}
fromVC.presentViewController(activityVC, animated: true, completion: nil)
problem starts here, UIActivityItemSource methods not being call
override func activityViewController(activityViewController: UIActivityViewController, itemForActivityType activityType: String) -> AnyObject? {
switch activityType
{
case UIActivityTypeMail:
return msg
case UIActivityTypeMessage:
return msg
case UIActivityTypePostToFacebook:
return msg
case UIActivityTypePostToTwitter:
return strTwitterShare
default:
return msg
}
}
thanks for help
It should work if you use let activityVC = UIActivityViewController(activityItems: [self], applicationActivities: nil)
... then your message is provided inside your itemForActivityType method.

Unexpectedly found nil while unwrapping an Optional value in For In Loop

I'm trying to read a CBCharacteristic value from a found CBService, and I keep getting the error
fatal error: unexpectedly found nil while unwrapping an Optional value
My code is as follows
func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!)
{
for characteristic in service.characteristics as! [CBCharacteristic]
{
peripheral.readValueForCharacteristic(characteristic)
}
}
Check to ensure the CBService is not nil, and that appears to be sound. Any know the proper swift way to unpack this list?
Thanks DogCoffee!
func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!)
{
if let characteristics = service.characteristics as? [CBCharacteristic]
{
for characteristic in characteristics
{
peripheral.readValueForCharacteristic(characteristic)
}
}
}

Resources