Accessing SBElementArray content from Scripting-Bridge in swift - swift

I've been trying to use, in a swift code, the various SBElementArray generators defined in the iTunes.h ScriptingBridge header, for example:
List of playlists: (SBElementArray<iTunesPlaylist *> *) playlists;
List of artworks associated to a track: (SBElementArray<iTunesArtwork *> *) artworks;
But when i try to use a method associated to the type contained in those array:
let playlists: SBElementArray = iTunes.playlists()
if let playlist = playlists[0] as? iTunesPlaylist {
print(playlist.name)
}
I get a compile error:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_iTunesPlaylist", referenced from:
objc-class-ref in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This seems to be limited to the SBElementArray as I have no problem accessing current track name with the following :
let track: iTunesTrack = iTunes.currentTrack;
print(track.name)
I'm also guessing that it has something to do with the type casting I'm trying to do from 'anyObject' to 'iTunesPlaylist' in my code (which i think i need to be bale to access the playlist content or whatever artwork i would like to display), because the following code:
let playlists: SBElementArray = iTunes.playlists()
print(playlists[0])
print(type(of: playlists[0]))
corectly returns:
<ITunesPlaylist #0x6080000402d0: ITunesPlaylist 0 of application "iTunes" (93931)>
ITunesPlaylist

The downcasting is trying to create an optional and initialize an iTunesPlaylist that is not allowed and not present in the linker. Force it as you always know it to be a iTunesPlaylist or check the type.
if playlists.count > 0 {
let playlist : = playlists[0]
if playlist is iTunesPlaylist {
print(playlist.name)
}
}

Related

realm swift: how could I trap on object being invalidated?

I'm attempting to add a watcher for isInvalidated on an object of interest like so:
cancellable = images.publisher(for: \.isInvalidated, options: [.new])
.sink { newValue in
print("property changed")
}
which yields
Undefined symbols for architecture x86_64:
"property descriptor for (extension in RealmSwift):__C.RealmSwiftEmbeddedObject.isInvalidated : Swift.Bool", referenced from:
l_keypath in ContentCardView.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
at link time.
How would I add a [programmatic] watchpoint for invalidation of an object in realm?
PS a simpler way out via lldb does not work either
(lldb) watch images.isInvalidated
error: command 'watchpoint' did not recognize 'images .isInvalidated' as valid (subcommand might be invalid).
(lldb) watch images.invalidated
error: command 'watchpoint' did not recognize 'images .invalidated' as valid (subcommand might be invalid).
(lldb) wa s v images.invalidated
error: "invalidated" is not a member of "(Foo) images"
(lldb) wa s v images.isInvalidated
error: "isInvalidated" is not a member of "(Foo) images"

Swift: Type cast results in 'Symbol not found'

In a Swift test bundle, I use the following line to get a class object of a class, which is defined in a dynamically loaded bundle (MyPlugin). The bundle contains Swift and Objective-C code and is loaded successfully before the following command is executed:
let aClass = NSClassFromString("MyPlugin.BackendAPIManager") as! BackendAPIManager.Type
When the test tries to execute the line, I get the following runtime error:
dyld: lazy symbol binding failed: Symbol not found: __TMaC16MyPlugin17BackendAPIManager
When changing the line to
let aClass = NSClassFromString("MyPlugin.BackendAPIManager") as! NSObject.Type
I can use
let apiURL = aClass.perform(#selector(BackendAPIManager.apiUrl(path:)), with: "/something")
which means that the class is correctly loaded through the bundle, but somehow I cannot access the class through its name. The corresponding symbol is not found and I have no clue what could cause this. Any ideas?

HTHorizontalSelectionList unexpectedly found nil while unwrapping an Optional value

..and I'll tell you why:
I'm using the following pod: HTHorizontalSelectionList
If I declare it like this:
class RightViewController: UIViewController, HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource {
var selectionList: HTHorizontalSelectionList!
}
I get the following error on compilation:
ld: warning: directory not found for option '-FTest'
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_HTHorizontalSelectionList", referenced from:
__TMaCSo25HTHorizontalSelectionList in RightViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Huh!? What!?
If I instead implement it like this it compiles fine!
override func viewDidLoad() {
super.viewDidLoad()
var selectionList: HTHorizontalSelectionList!
selectionList?.frame = CGRectMake(0, 0, self.view.frame.size.width, 40)
selectionList?.delegate = self
selectionList?.dataSource = self
self.view.addSubview(selectionList)
}
...except of course, I get an error on the addSubview line:
fatal error: unexpectedly found nil while unwrapping an Optional value
I'm finding it incredibly difficult to understand how Swift works when I get stuff like this happening quite often.
I'm finding it incredibly difficult to understand how Swift works when I get stuff like this happening quite often
There is nothing difficult about this. You are starting out with your Optional variable set to nil. It stays nil. Eventually you try to unwrap nil and then you crash, because you can't do that:
var selectionList: HTHorizontalSelectionList! // it is nil
selectionList?.frame = CGRectMake(0, 0, self.view.frame.size.width, 40) // still nil, nothing happens
selectionList?.delegate = self // still nil, nothing happens
selectionList?.dataSource = self // still nil, nothing happens
self.view.addSubview(selectionList) // unwrap, crash
If you don't want to crash, assign selectionList an actual value other than nil, like maybe an actual HTHorizontalSelectionList.

Dictionary updateValue causes swift compiler segmentation fault

My class has a property
var activeSquares = Dictionary <String, SKShapeNode> ()
I try to add and update values in the dictionary using
let squareNode = SKShapeNode(rectOfSize: CGSizeMake(80, 80), cornerRadius: 8)
activeSquares.updateValue(squareNode, forKey: "someUniqueDescription")
I also get the same crash if I use
activeSquares["someUniqueDescription"] = squareNode
But it is causing a crash when compiling
1. While emitting IR SIL function #_TFC14gamename9GameScene11addedSquarefS0_FCS_6SquareT_ for 'addedSquare' at /.../gamename/gamename/GameScene.swift:30:5
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: swift frontend command failed due to signal (use -v to see invocation)
Command /Applications/Xcode6-Beta2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254
How do I properly use updateValue to add/update key/value pairs to my dictionary?
No sure if my solution fits here, but it could help. Seems there's something weird with subscription for NSDictionary. Not sure why, but it's defined as:
func objectForKeyedSubscript(key: NSCopying!) -> AnyObject!
so, if I'm not wrong it returns implicitly unwrapped optional, but should return optional, as far as there could be no value for a key. If you try to write:
if(dictionary["key"] != nil)
you'll get a compile error "AnyObject is not convertible to UInt8" while if you write:
if(dictionary["key"])
you don't get one.
The way I solved this issue was using optional unwrapping, so:
if(someBool && dictionary["key"]) // fine in beta 2, crash in beta 3-4
// turned to:
var hasValueForKey = false
if dictionary["key"]
{
hasValueForKey = true
}
if(someBool && hasValueForKey) // fine in beta 4
and:
var someArray : NSArray? = dictionary["key"]? as? NSArray // note ? after []
I guess there could be something with optionals and stuff for setting an object via subscript, it's defined as:
func setObject(obj: AnyObject!, forKeyedSubscript key: NSCopying!)
Maybe playing with optional unwrapping could help here as well.

iPhone - isKindOfClass generates a linker error

I have a class defined like this :
#interface ServerCall : NSObject {
// vars
}
// properties
// functions (init)
#end
and
#interface ServerNotification : ServerCall {
// just vars
}
#end
I have a call in another class :
if ([call isKindOfClass:[ServerNotification class]])
// do things
When building I have this error :
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_ServerNotification", referenced from:
objc-class-ref in ServerCommunication.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
"_OBJC_CLASS_$_ServerNotification", referenced from:
objc-class-ref in ServerCommunication.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
I also have in build settings "Symbols hidden by default" = NO
What did I miss ?
The linker doesn't find implementations for your ServerNotification class. Do you have the matching implementation file added to your target?