How to convert URL into Unmanaged<CFURL> using Swift? - swift

In Swift I am trying to create an AUSamplerBankPresetData object which requires an Unmanaged<CFURL> object.
How do I convert a URL object into Unmanaged<CFURL> in swift?

You first need to convert your URL into a CFURL. This can be done with an unconditional cast so:
let cfurl = url as CFURL
Then, to create an unmanaged reference to that CFURL you'll need to use one of the functions outlined here to create an Unmanaged object. Make sure you choose the right one for your application. The example below will increment the reference count, so you will need to make sure that it is decremented later.
var um = Unmanaged<CFURL>.passRetained(cfurl)

Related

Swift - Empty NSMutableDictionary or NSDictionary? Optional

Just curious, in Swift, is it more ideal to initialize an empty NSMutableDictionary variable, NSMutableDictionary = [:], and later re-assign its value to a new dictionary (coming from an API for example),
OR, is it better to declare an optional NSDictionary, NSDictionary? and assign it to a new dictionary?
So with Swift it would technically be best practice to use a Dictionary type. Like this for example:
var dict: Dictionary<String, Int>
If you need the dictionary as a whole to be able to be nil use an optional.
This depends on your needs, do you want it to be nil sometimes? is it nil sometimes?
If an array is always gonna have value, even if it's an empty value, I personally like to Initialize it right away, and not hassle with unwrapping everywhere.
Maybe if you had two arrays, one was normal array, and the second one was a searched result. You might wanna check if searched result is nil first, if it is, show the array1, if it isn't show it instead.
And this is implying you only search "sometimes", thus that array is only sometimes used - so you might as well have that deallocated when not in use, if you aren't using it most of the time.
EDIT: I've been using arrays in my example, but same applies for a dictionary in those situations.
EDIT: In Swift It's best to avoid 'NS' classes, sometimes you have to use them, sure. But Swift's Dictionary does the job.
Example:
var sometimesUselessDict: Dictionary<String, AnyObject>?
var alwaysUsedDictionary = Dictionary<String, AnyObject>()
Cheers
You should make it optional only if you need to be able to distinguish a dictionary that's empty from one that doesn't exist at all. For instance, if you're receiving data from a server, you might want to distinguish between a successful response that returned no data (empty dictionary) and a failed or invalid response (nil).
If that distinction isn't important, I would always go with a non-optional to avoid unnecessary unwrapping.

How do you know when to cast an item in swift?

I see it done all the time but don't know how other coders know when to cast an item. Here is an example that recently had me wondering how the coder knew to cast the item:
let item = NSEntityDescription.insertNewObjectForEntityForName("Item", inManagedObjectContext: ad.managedObjectContext) as! Item
The insertNewObject(forEntityName:into:) API that you're describing returns a NSManagedObject, which is the way new managed objects are created, configured and returned to you to make use of.
In CoreData, all NSManagedObjects saved are actually subclasses of the NSManagedObject base class, so if you want to let item = from that call, you'll need to cast it to the actual subclass type it's supposed to be.
Makes sense?

How to open a file in binary form in Swift

I'm new to Swift and I'm not sure how I can pick up a file and store it in a binary array.
I do know how to pick up a file, but I don't know how I can store it in a binary array which would be modified later.
Suppose the variable "chosenFile" is the file I pick up ( in NSData type)
And the variable "bArray" ( [int8] array) is the array used to store the binary representation of the file.
var bArray: [Int8] = [Int8]()
var chosenFile: NSData! = NSData(contentsOfURL: "xxxxxxxx")
Any help ?
If you create an instance of NSMutableData, you can use the mutableBytes property to get a reference to the underlying data. In C, this would be a void *; in Swift, it is an UnsafeMutablePointer<Void>.
You can then work with this data directly (there's no need for another byte array). So long as you stay within the length of the NSMutableData, you can, eg, replace individual bits of data, or indeed the entirety of the data.
Alternatively, rather than working on the primitive directly, you can use replaceBytesInRange:withBytes: to selectively modify part of your data. This is also explained in the documentation.

'CGImageSource' does not have a member named 'takeUnretainedValue'

A lot of posts on the internet say I can return an unretained image source with the following code:
let imageData1 = image.TIFFRepresentation
let source1 = CGImageSourceCreateWithData(imageData1 as CFDataRef, nil).takeUnretainedValue()
let maskRef1 = CGImageSourceCreateImageAtIndex(source1, UInt(0), nil)
However, I get the following compile time error:
'CGImageSource' does not have a member named 'takeUnretainedValue'
Am I doing something wrong? or has this method been removed in favour of something else?
You only use takeUnretainedValue or takeRetainedValue when dealing with an unmanaged object (e.g. some function that returns a Unmanaged<AnyObject>! or something like that). In this case, this isn't an unmanaged object, so this is not needed.
By the way, when returning a object from a Core Foundation function with Create or Copy in the name, the ownership of this object has been transferred to you, and you are responsible for releasing it (see the Create Rule). In these cases, you almost always want to use takeRetainedValue, so that the memory management of this object is managed automatically by Swift's ARC implementation. If you use takeUnretainedValue, you will leak memory unless you manually release the memory associated with this object that was obtained via a method conforming to the Create Rule.

ios iphone app - set value to a variable in init function

I use sdwebimage and ktphotobrowser for my thumbnail gallery. because I have different galleries I need many sources. But i can't change the datasource in the SDWebImageDataSource.m file
I made a variable named myStr and now i want to get it in the SDWebImageDataSource's init function so i can get data from another source
Is it possible or may I have a change create another function named initWithString then pass my path to that function?
in my viewcontroller I initialize the SDWebImageDataSource as
SDWebImageDataSource *sd = [[SDWebImageDataSource alloc]init];
then i set variable
sd.myStr=#"http://mypath.com";
but I can't get the value of myStr in the init function of SDWebImageDataSource.m file
In Init method, initialize your string by setting an empty string. This will solve your problem. or if you can manage to create custom method [[SDWebImageDataSource alloc]initWithString:#"SOMESTRING"];will also be fine.