What is the Core Foundation equivalent code for the Cocoa method +[NSData dataWithContentsOfFile:]? - core-foundation

I'm using Core Foundation functions. What is the Core Foundation equivalent code for the Cocoa expression [NSData dataWithContentsOfFile:filePath]?

Create a CFURL referring to the file and ask it for the data.

Related

import Swift vs import Foundation

Question
What is the difference between import Swift and import Foundation?
Until I read this comment by Martin R, I didn't even know that there was an import Swift.
Reading
I couldn't find the documentation and doing a Google search didn't turn up much.
What I tried
Testing it out shows that import Swift does not give any compile errors, but that doesn't really answer my question.
If I were to guess, I would say that you import Swift for Swift projects and that you import Foundation for Objective-C projects or maybe for Swift projects that use Objective-C classes (like NSString).
Testing this in the Playground:
import Foundation
import Swift
var str = "Hello, playground"
let str2: NSString = "hello"
let str3: String = "hello"
Commenting out import Swift gives no errors and str is of String type. However, commenting out import Foundation gives an "undeclared type" error for NSString.
My question revisited
I would be happy enough to abandon Foundation and just use Swift. So am I right to just import Swift all the time unless I specifically need to use one of the old Objective-C classes?
Yes, you will only need import Foundation if you want to access NSObject or one of its subclasses. Foundation is the framework that brings in that class hierarchy. However, it's highly likely that in a project you'll need more than just import Swift. Like Rob commented, import UIKit is also a nice option.
In case you haven't read it already, Apple explains the Foundation framework here.
If you want to work with Strings, Dates, etc you need to import Foundation.
The Foundation framework provides a base layer of functionality for apps and frameworks, including data storage and persistence, text processing, date and time calculations, sorting and filtering, and networking.
If you want to work with UITableViewController, UIAlertController you need to import UIKit.
If you import UIKit you do not need to import Foundation because it already imports it in the backstage.
The Swift standard library defines a base layer of functionality for writing Swift programs, including:
Fundamental data types, Common data structures, Global functions such as print(:separator:terminator:) and abs(:), Protocols, such as Collection and Equatable... etc
If you import Foundation, then no need to import Swift again as Foundation contains references to Swift Standard Library by default.
When you are writing something not for iOS Apps, like say a server programming based on Vapor , you may need to consider import Swift.
Refer:-
https://developer.apple.com/documentation/swift/swift_standard_library/
Pleases refer:-
https://hasancan.tech.blog/2018/01/17/import-foundation-vs-uikit/
import Foundation used for access NSObject or one of its sub class.NSObject means we can extend our class using objective C runtime features.But import UIKit or import swift, that is automatically generate while we create our xcode project

Swift - Shared Core Data in Framework - auto-generated classes aren't public?

I'm playing around with sharing a Core Data model in an App Group for a WatchKit app in Swift, loosely following/mimicing this guy's work. I've created a custom framework (called CoreDataKit) and put the .xcdatamodeld file to be a member only of the framework target; I've created a CoreDataStack object which creates and manages the core data stack, which is also a member only of the framework target. Finally, I've created a new entity in the data model, and then used Xcode to auto generate the Swift file for that entity, which gives a class structure that looks like:
import Foundation
import CoreData
class FlightStatus: NSManagedObject {
#NSManaged var altitude: NSNumber
}
When I import CoreDataKit into my view controller in the iOS app to start doing Core Data stuff, I'm able to access the CoreDataStack, but not the FlightStatus class - I get a Use of undeclared type 'FlightStatus' error.
When I go into the FlightStatus.swift file and make it a public class, however, the error goes away. But this makes me a little uncomfortable - I've been trained never to touch the auto-generated Core Data classes, because they may need to be regenerated at any time, and therefore I tend to add helper methods and the like in categories on the auto-generated Core Data classes.
Similarly, I can't access the altitude variable unless I make it public.
Am I going wrong somewhere? Is there a way I can ensure that my auto-generated Core Data entity classes can be visible when the framework is imported without having to manually add the public keyword to them?
Make sure to add #objc(CoreDataClassName) in your class declaration. Modifying your original code slightly:
import Foundation
import CoreData
#objc(FlightStatus) class FlightStatus: NSManagedObject {
#NSManaged var altitude: NSNumber
}
EDIT: I just saw that you mentioned in the comments of your original post that doing this didn't help. Did you follow the above format exactly? Also, regarding your intuition that you shouldn't mess with the CD auto generated files -- Apple is clearly still working out some of the kinks with Swift's Core Data integration, so unfortunately a bit of tinkering is often required. (Another example: Core Data doesn't autogenerate any support for optionals at the moment.)

Is it ok to store any Core Foundation type in NSMutableDictionary by casting it to id?

Is there any risk in storing any Core Foundation type in NSMutableDictionary by simple casting it to id?
As CoreFoundation objects are not compatible with ARC, you need to do its release and autorelease yourself. Also you need to bridge cast it by using these, as simply casting to id may lead to some problem:
__bridge
__bridge_transfer
__bridge_retained
NS collection types are used to contains objC objects, CF types aren't objC objects. What you can do is use toll free bridging and ARC bridging where you can, but not all CF types have a corresponding class in Foundation.
I never tried but I think that you can save just the pointer of a CF object inside an NSValue and later add it to a collection, but be aware about memory management.

iOS APIS - 'Reference'

In iOS, we have CGContextRef, CGFontRef and such. What do 'Ref' indicate here? Are they refernces to objects of type CGContext, CGFont ? I do not understand this particular concept. Where can I look for the explanation of this concept?
Any help appreiated,
As far as I know, CGContextRef, CGFontRef and such are pointer types (typedefs) to plain C structures, not to Objective-C objects, and they are coming from older C frameworks of Mac OS X. See CGContext reference, CGFont reference, Core Foundation Design Concepts and Memory Management Programming Guide for Core Foundation. Because those *Ref objects are ``toll-free bridged'' with the Objective-C Cocoa framework, you can substitute them for Cocoa objects in function arguments, and vice versa (3). They have their own rules for memory management (4). I myself got a first brief but mostly sufficient explanation of those Core Foundation objects and frameworks by watching the Stanford iPhone Programming class videos (see also the class' site).
Hope I gave you some pointers to start with.
Cmd+Click on the desired type:
/* The type used to represent a CoreGraphics font. */
typedef struct CGFont *CGFontRef;
There is a struct CGFont. typedef keyword makes alias CGFontRef for this struct. 'Ref' indicates that it's a pointer.

In Core Foundation, do you have to access objects only using references?

I am trying to write a bit of Objective-C (which is new for me, my background is more C++), and wanted to create a CFString like this :
CFString myString;
When I try to build my project the following error :
"'CFString' undeclared" (first use in this function)
prevents from building.
I thought I had just forgot to include the relevant header, but I cannot find which one to include. When I look into some sample code I never see "CFString" but rather "CFStringRef" objects, defined as
A reference to a CFString object.
typedef const struct __CFString *CFStringRef;
I started to suspect there is no such thing as a CFString that i can refer to in the code, but I feel I am wrong somewhere. Am I ?
Is it impossible to create CFStrings ? Is it specific to CFString or to all the structs in objective-c ?
CFStringRef is part of the Core Foundation library which is not ObjC but plain C. Core Foundation uses a runtime to create instances, this runtime will call callbacks to initialize the structs content and manages the instances memory.
This is why there is no CFString because you shouldn't create static instances on the stack but rather call the appropriate create function which will then ask the runtime for a new instance with everything already initialized for you. For CFStringRef this would be CFStringCreate(), see also: http://developer.apple.com/library/ios/#documentation/CoreFoundation/Conceptual/CFStrings/introCFStrings.html

Resources