Metal iOS gives compile error - ios

import UIKit
import Metal
import QuartzCore
class ViewController: UIViewController {
var device: MTLDevice! = nil
var metalLayer: CAMetalLayer! = nil
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
device = MTLCreateSystemDefaultDevice()
metalLayer = CAMetalLayer() // 1
metalLayer.device = device // 2
metalLayer.pixelFormat = .BGRA8Unorm // 3
metalLayer.framebufferOnly = true // 4
metalLayer.frame = view.layer.frame // 5
view.layer.addSublayer(metalLayer) // 6
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
When I have this in my ViewController.swift, I get the error "Use of undeclared type CAMetalLayer" even though I've imported Metal and QuartzCore. How can I get this code to work?

UPDATE:
Simulator support is coming this year (2019)
Pre Xcode 11/iOS 13:
Metal code doesn't compile on the Simulator. Try compiling for a device.

If your app has a fallback or mode that doesn't depend on Metal, and you want to compile your app for the simulator, you can do something like this:
#if targetEnvironment(simulator)
// dummy, do-nothing view controller for simulator
class ViewController: UIViewController {
}
#else
class ViewController: UIViewController {
var device: MTLDevice! = nil
var metalLayer: CAMetalLayer! = nil
override func viewDidLoad() {
super.viewDidLoad()
device = MTLCreateSystemDefaultDevice()
metalLayer = CAMetalLayer()
...
}
}
#endif
Then your code will at least compile for both device and simulator, which can ease your non-Metal development.

The same problem may appear if you name your XCode project "Metal".
In that case compiler will be confused and you'll receive the same error message.

Related

Value of type 'PHPhotoLibrary' has no member 'presentLimitedLibraryPicker' - Xcode 11.3.1

Xcode 11.3.1
Use of undeclared type 'PHAccessLevel'
Value of type 'PHPhotoLibrary' has no member 'presentLimitedLibraryPicker'
I'm getting this error only if I try to compile against an actual device. Tried with both iOS 14 and iOS 13 devices. Code compiles with simulators.
Tried clean building, cleaning Dervied Data and closing and reopening the project. But no luck
import Photos
import PhotosUI
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if #available(iOS 14, *) {
let accessLevel: PHAccessLevel = .readWrite
let authorizationStatus = PHPhotoLibrary.authorizationStatus(for: accessLevel)
switch authorizationStatus {
case .limited:
var testVc = UIViewController()
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: testVc)
//callBack(authorizationStatusAuthorised)
case .authorized:
//callBack(authorizationStatusAuthorised)
var te = ""
default:
//callBack("")
var test = ""
}
}
}
}
You are using an iOS 14 API feature (i.e. PHAccessLevel) while using iOS 13.x SDKs, as you're using Xcode 11.
You need Xcode 12 and above to make use of it.
Source: https://developer.apple.com/documentation/photokit/phaccesslevel

Swift Webview Error - Audio presentation output context not supported by FigEndpointPicker

I have an issues with my swift application. I'm using UIWebview and I been getting the following error:
[AVFigEndpointPickerOutputContextImpl sharedAudioPresentationOutputContext]: Audio presentation output context not supported by FigEndpointPicker
and Thread eroor
Thread 4: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
Here is my ViewController.swift code
import UIKit
class ViewController: UIViewController {
#IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
//UserDefaults.standard.register(defaults: ["UserAgent" : "Chrome Safari"])
// Do any additional setup after loading the view, typically from a nib.
let url = URL(string:"http://example.com/")
let urlReq = URLRequest(url:url!)
webView.loadRequest(urlReq)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I got the same issue with my simulator on Xcode 9 beta 3.
The error detail here
[AVFigEndpointPickerOutputContextImpl sharedAudioPresentationOutputContext]: Audio presentation output context not supported by FigEndpointPicker
The bug did not occur on the older version of Xcode. I will report this issue to Apple through Bug Reporter.
Fixed the issue. I was running xCode 9 Beta with iOS 11.0 Beta. So basically I was set up to fail from the beginning.
I reinstalled my xCode to version 8 and I'm using iOS 10, and it works.

iOS web view error

Im trying to play a Youtube video in my app with the Webview
the build is succsesful but when i run the app it shows all white and i get this error: Thread 1:EXC_BAD_INSTRUCTION(code=EXC_I1386_INVOP,subcode=0x0)
it happenes on the Youtube.loadRequest(URLRequest(url:url!))
//
// ViewController.swift
// CFBC
//
// Created by KWIA on 6/6/17.
// Copyright © 2017 KWIA. All rights reserved.
//
import UIKit
import Firebase
class ViewController: UIViewController {
#IBOutlet weak var Youtube: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getVideo(videoCode: "RmHqOSrkZnk")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func getVideo(videoCode:String)
{
let url = URL(string: "https//www.youtube.com/embed/\(videoCode)")
Youtube.loadRequest(URLRequest(url: url!))
}
}
Your URL is not valid, you are missing : after https.
It should be: "https://www.youtube.com/embed/\(videoCode)"
That leads to URL(string:) returning nil and to a crash when you try to force unwrap it using !.

Libpq framework for swift iOS to work with postgreSQL

I've been trying to connect my postgreSQL database to an iOS app on swift for quite a while now. I managed to connect it for macOS development by creating Obj-C Bridging header for system provided libpq framework, but macOS development is unusual for me and I want this project to be done well. I found a libpq framework for iOS Obj-C, tried to access it through Bridging Header, but server is not responding to my queries.
I put this within my bridging header
#import <libpq/libpq-fe.h>
And this is my view controller code
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let conn = PQconnectdb("postgresql://localhost/investProject".cString(using: .utf8))
if PQstatus(conn) == CONNECTION_OK {
let result = PQexec(conn, "SELECT version()")
for i in 0 ..< PQntuples(result) {
guard let value = PQgetvalue(result, i, 0) else { continue }
let dbname = String(cString: value)
print(dbname)
}
PQclear(result)
}
PQfinish(conn)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Compiler recognizes all the methods, output is supposed to be the version of my db, but it's empty instead.

Xcode 7 beta 2 and swift not getting along

I have an error when I typed in some code, it gave me an error. It looks perfect, but here is the code:
import UIKit
class SecondViewController: UIViewController {
#IBOutlet weak var resultLabel: UILabel!
#IBOutlet weak var tempText1: UITextField!
#IBAction func convertTemp1(sender: AnyObject) {
#let fahrenheit = (tempText1.text as NSString).doubleValue
let celsius = (fahrenheit - 32 )/1.8
let resultText = "Celsius \(celsius)"
resultLabel.text = resultText
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
It give me the following error: 'String?' is not convertable to 'NSString' On the line I put a # on. The # is not in the real code.
Note: I do not understand much computer talk so please try to speak very simply. :)
The text property of UITextField returns an optional string but your code doesn't handle optionals. Casting to NSString isn't allowed there (also not necessary to get to the doubleValue).
You need to handle the optional. For this you could force-unwrap it using !. But that can lead to crashes. It would be better to use the if let or guard let statements:
guard let fahrenheit = tempText1.text?.doubleValue else { return }
For conciseness we use optional chaining (the ?here). We could also keep this in two steps:
guard let fahrenheitString = tempText1.text else { return }
let fahrenheit = fahrenheitString.doubleValue
Both are basically equivalent.

Resources