Something odd has started happening with a new Swift project I've created
You can see that I have a class called ViewController which is of type UIViewController
I have a function called someFunction that takes an input String? and give a String of "ABCDEFG". Simple right?
Well when I try to assign a class variable the output of that function you can see that autocomplete thinks someFunction requires (self: ViewController) input and it should instead take a String? input
Does anyone know what is happening here? It's driving me crazy
So it turns out I derped pretty hard.
I was trying to utilize a member only function from within a scope where the class hadn't been instantiated.
Instantiate your classes first kids...
Related
I'm new to Swift and programing in general, but my function before I updated to the latest xCode was
func setPressedAction(action: () -> ()) {
self.pressedAction = action
}
With the update I am getting
Method 'setPressedAction' with Objective-C selector 'setPressedAction:' conflicts with setter for 'pressedAction' with the same Objective-C selector
How do I fix this?
Thanks!
You have a pressedAction property. You can't declare an instance method named setPressedAction under that situation, because in Objective-C's mind, that's the setter for the property. Just give the function a different name.
The reason this didn't emerge until the update is that Apple fixed a bug (closed a loophole) so that you couldn't make this mistake accidentally - as you were doing.
I created a Service for my app, the menu appears correctly, but I still have a problem with the method called when the menu item is activated.
In AppDelegate.swift I have:
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSApplication.sharedApplication().servicesProvider = ServiceProvider()
NSUpdateDynamicServices();
}
And the class ServiceProvider is:
import Cocoa
class ServiceProvider {
func serviceTest(pasteboard: NSPasteboard, userData: String, error: NSErrorPointer) {
// code here
}
}
When the service is activated I want to send some information to the components of my app, for example, to change the text of a label. How can I do this? I tried to get the mainWindow inside the function serviceTest to access its components, but it returned nil.
I am trying to do something similar to the Safari service that you select some text, and right-click > services > search with google. This open Safari and search for the selected text. Or something similar to add a new task to Wunderlist, that gets the selected text and create a new task in the app.
I really appreciate some help to solve this issue.
This is just the general problem of getting access to objects in various parts of a program. There are two general mechanisms: start with a well-known object or have the object(s) you need passed in to you by the part of the code which knows about both object.
For example, you can get to the application delegate using NSApplication.sharedApplication().delegate. You would then cast it to your custom app delegate class and access its properties.
However, it's probably better to avoid relying on pseudo-global variables if there's a simple alternative, and there is. Give your ServiceProvider class the necessary properties to track the objects it needs (like the app delegate). In the app delegate code which creates the ServiceProvider instance, simply set those properties. That may be in statements to set properties after it has been created or you could pass it in to the initializer method as an argument. Obviously, you'd need to declare that initializer method to take the argument and store it in your instance variable.
I'm a bit confused why my NSThread cannot be instantiated using a selector due to a runtime error
target does not implement selector (*** -[FileSearcher processFilesAsync:])
The function is defined like this
func processFilesAsync(#data: [String])
and the NSThread will be created this way:
NSThread(target: self, selector: "processFilesAsync:", object: itemsPerThread[i])
"itemsPerThread" is just a dictionary with a String-Array as values.
As far as I know this should work as the method I want to invoke defines an argument and the selector I pass into NSThread's init() method indicates that the target method expects exactly one argument.
I already tried using Swift's "Selector" type instead of only a string but this didn't work as well. I also tried to change the method's type from "[String]" to "AnyObject" which didn't work, too.
Does anybody have a clue what might be wrong?
All of the code above lies within the same class.
I'm using Xcode 6 Beta 5.
Edit
I figured out that is has to do something with the parameter. I implemented a dummy method without parameters and tried to set this as target and it worked. As soon as I modified it to use a parameter as well -> same as above.
Found it. All I had to do was to change the parameter's type from AnyObject to AnyObject?. It even works with specialised types (in my case [String]? instead of [String]).
Of course, because you can call it nil as parameter...
I'm not sure what's expected for me to leave here, but basically, I've passed an object of type AwesomeMenu into an ActionControl object's (subclass of NSOBject) initializer class so that the ActionControl object has a reference to the AwesomeMenu. However, in one of the ActionControl functions, there is a call like
[self.menu updateButton];
Where self.menu is the AwesomeMenu and updateButton is a function within AwesomeMenu. For some reason, XCode never enters updateButton. I've tried setting up breakpoints inside updateButton. They don't ever trip. I tried stepping INTO updateButton (it just shows me the parameters and then it skips past the line without taking me into the function), etc. I don't get any errors either. My chosen path through the program takes me over that function call multiple times but it never actually calls.
What's happening?
I did not assign self.menu prior to calling one of its functions; self.menu's value was nil. I just had to switch my initialization statements around to get it to work.
I have been trying to use BLToolkit to activate an Oracle stored procedure which takes a User Defined Type as an argument as an output parameter and changes it.
I have managed to do this on a primitive type, and and also by manually calling SetSpCommamd however I would like to use the abstract class generation method but can't seem to get it to work.
I'm pretty sure the code I wrote is correct (works for the primitive). When debugging I found the SetSpCommamd called by the generated code gets wierd parameters instead of the ones I provided as opposed to when I call the method manually (the it gets the exact parameters I'd like). I wish I could see the code generated by the reflection emit to see what's wrong there.
Can anyone please help me figure out why this is not working?
Found the problem (Potentially a bug in BLToolkit).
BLToolkit does not pass the UDT Class as is to the procedure (instead it tries to flatten it or something and pass the insides of the object). I Changed the object to a Struct instead of a Class and that fixed it.
Later on I also changed it back to class and made a patch in the 'IsScaler()' method in BLToolkits code.
I will report this as a Bug I hope they fix it.