I am looking at a camera tutorial for IOS and the CGDataProviderCreateWithCFData function was used. However Xcode doesn't seem to recognize it. Was this function removed from the latest Xcode Swift update? I am using Xcode version 8.1 and Swift 3. Also I have the following imports:
import UIKit
import AVFoundation
Have you tried searching "CGDataProviderCreateWithCFData" in the Apple's API reference page?
When I tried, I was guided to the Objective-C page, and its Swift link shows:
Initializer
init(data:)
Creates a data provider that reads from a CFData object.
In Swift 3, CGDataProviderCreateWithCFData is imported as an initializer of CGDataProvider and use it as:
let dataProvider = CGDataProvider(data: imageData as CFData)
(Assuming imageData as a Data.)
You can use it with import UIKit.
Related
I have a Swift 5.5 iOS project that already contains Firebase 7.x and uses Firebase Functions. I just upgraded to 8.13.0, because I want to use Codable support in Firebase Functions.
I added Firebase through the Swift Package manager and included Firebase Functions in my source code using:
import FirebaseFunctions
However, when I want to call:
func httpsCallable<Request: Encodable,
Response: Decodable>(_ name: String,
requestAs: Request.Type = Request.self,
responseAs: Response.Type = Response.self)
I get an error saying that it doesn't find this method.
Upon inspecting the packages, I discovered that the function I want to call is defined in FirebaseFunctionsSwift in a file called Callable+Codable .
This exists in my project as you can see:
However when I add import FirebaseFunctionsSwift to my source file I get the following error:
No such module 'FirebaseFunctionsSwift'
How can I solve this problem?
Add FirebaseFunctionsSwift-Beta framework to the build target:
I'm getting an error for this line of Swift code in my XCode playground:
print(String(format: "%.2f", 3.345))
The error reads
No exact matches in call to initializer
I believe this means that I haven't used the right parameter names/order to call the initializer. However, when running this line of code while working on an iOS app or even in the online Swift playground http://online.swiftplayground.run/, the line runs without any issues.
When running it in the XCode playground or on my terminal through the Swift REPL however, it throws an error.
Why is this the case?
Foundation has to be imported before using String.
String is a Foundation type in swift.
I was corrected by Matt below in the comments.
String is built into swift and String(format: is in foundation.
I guess that's why the docs didn't show it as such.
Thank you for the correction.
I have an app created from Xcode 9 / New Project / Cross Platform Game App, targets: macOS and iOS.
I wrote the following simple function to load text from a Data Asset.
func dataAssetAsString(_ name: String) -> String? {
if let asset = NSDataAsset(name: NSDataAsset.Name(name)) {
return String(data: asset.data, encoding: .utf8)
}
return nil
}
I'm puzzled by the following things:
This function only works if I import either UIKit, or AppKit.
But somehow importing MetalKit instead of UIKIt or AppKit makes it work. This really puzzles me. This should have nothing to do with MetalKit.
Most answers I found for code sharing between iOS and macOS suggest some kind of conditional import at the beginning of the file / shimming.
I went through the slides of WWDC 2014 Session 233: Sharing code between iOS and OS X, and it explicitly says we should not do shimming in Swift.
On the other hand, I don't understand what we should do instead of shimming. The mentioned very complicated process of creating and compiling shared frameworks for a 5 line function cannot be the right direction.
In 2018 (Swift 4, Xcode 9, iOS 11, macOS 10.13), what would be the recommended way to add such a trivial cross platform code to an app?
What is the magic of MetalKit which makes it work without shimming? Does it shim internally?
I am attempting to integrate the Yelp API into my iOS app. From the existing yelp documentation for objective C, we downloaded their Xcode project and dragged it into our project. However, when trying to call and import the project in my swift file, we have errors. Here is what I mean:
func getYelpData(mapItem:MKMapItem) {
var term: String = (NSUserDefaults.standardUserDefaults().valueForKey("term") ?? "") as! String;
var location: String = (NSUserDefaults.standardUserDefaults().valueForKey("location") ?? "") as! String
var APISample: YPAPISample = YPAPISample() // Use of undeclared type 'YPAPISample'
and at the top of the class I try to import the YPAPISample like so
import YelpAPISample
but it does not allow me to do this either.
I'm new to swift programming so any help would be greatly appreciated!
It's one of the most regularly asked question regarding Swift on SO.
Apple provided a document here for such purpose:
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
In short, you'd need to create a bridge header to import the Objective-C framework. Not all the framework can be import directly in Swift.
To add it manually, search bridging in build settings: (Make sure you select All)
Parse is acting very strangely in Yosemite, saveInBackground claims to not be a member of PFObject.
var score = PFObject(className: "score")
score.setObject("Mo", forKey: "name")
score.setObject(1, forKey: "scoreCount")
score.saveInBackground()
Clearly this should work, perhaps it's an issue with Xcode 6.0.1 or Yosemite GM3 (Or a combination). To be clear, using saveInBackgroundWithBlock works fine.
Has anyone else experienced this or a similarly weird bug?
The saveInBackground method is declared in the header to return a BFTask * object, which is part of the Bolts framework. Make sure your project is linking the Bolts framework, and then add
#import <Bolts/Bolts.h>
to your bridging header.
This solved a few "missing" APIs in Swift for me (this one, as well as PFAnalytics.trackAppOpenedWithLaunchOptions mentioned here: Why does my PFAnalytics not have trackAppOpeneWithLaunchOptions function? (IOS SWIFT)
If you don't want to mess around with the Parse framework files, you should replace:
score.saveInBackground()
with:
score.saveInBackgroundWithTarget(nil, selector: nil)
No need for bridging headers since release 1.0. To fix the issue, just add, import Bolts at the top of your class, below import Parse:
import Parse
import Bolts
With the added import statement, saveInBackground() should work as is.
ok, got it, in the parse,framework open up headers, go to PFObject.h and open it, navigate to where it says #name Saving Objects and write down this:
(void)saveInBackground;
run the app or close and open again Xcode and try to write down again the code and saveInBackground should be now an PFobject of your score.
this work
testObject.saveEventually()