16206

How to call my method in cocoa, self doesn't work

Question:

Working on a program that will record some things from the webcam when a user presses physical buttons connected to the mac via phidgets. Have call methods on other places in my app simply doing [self method: input], but on one place it doesn't work. What could be wrong?

This is the method i want to run if i get inputchange in my program.

Also i do -(void)reportButton2:(NSInteger)inputVal:(NSInteger)inputInd; in my .h file.

-(void)reportButton2:(NSInteger)inputVal:(NSInteger)inputInd { //NSLog(@"phidget för port = %%d med signal %%d", ind, val); if(inputVal == 1) { NSError* error; NSFileManager* deleteMgr = [NSFileManager defaultManager]; NSString* path = @"/Users/Shared/tempFile.mov"; [deleteMgr removeItemAtPath:path error:&error]; [mCaptureMovieFileOutput recordToOutputFileURL:[NSURL fileURLWithPath:@"/Users/Shared/tempFile.mov"]]; } else if(inputVal == 0) { [mCaptureMovieFileOutput recordToOutputFileURL:nil]; } }

The code below give me result if imput from the buttons change. Here i just can't seem to call reportbutton2.

If i try to use [self reportButton2..] in gives me "Use of undeclared identifier 'self'"

int gotInputChange(CPhidgetInterfaceKitHandle phid, void *context, int ind, int val) { what to do here? return 0; }

Answer1:

The problem is that gotInputChange is a C function not a Objective C method and so has no udea what self is as it does not belong to a class.

for [self reportButton2... = to work it needs to be a method in your class

Answer2:

I usually put something like this at the top of callbacks like gotInputChange:

MyObject *self = (id)context;

Then I can use self throughout the function as if it were a method.

The other thing being in a function makes harder is asserting conditions. The regular assertion macros, such as NSAssert and NSParameterAssert, require both of the implicit arguments to every method (self is one of them) to exist. In a C function, you must use NSCAssert, NSCParameterAssert, etc. instead.

Answer3:

You can also use your delegate.

SomeNameAppDelegate *delegate = (SomeNameAppDelegate *)[[NSApplication sharedApplication] delegate]; [delegate yourMethodName];

It works like in example if your target method in AppDelegate class. But when you have an access to delegate, you can create a pointers to necessary classes and use them over delegate.

Answer4:

Quickest but not soo nice way is to make your Class a singleton and access it from the gotInputChange function.

Answer5:

Okay thanks! Din't have any clue that it was C would never have solved it by myself. Did some googeling and this one did the trick for me.

[(id)context performSelectorOnMainThread:@selector(reportButton2:)withObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:ind], [NSNumber numberWithInt:val], nil] waitUntilDone:NO];

Recommend

  • Check full access for custom keyboard extension ipad
  • Logging from the IPhone: where is the file?
  • upload Zip file in AFNetwoking 3.0
  • Caching with UIImage and downloaded images
  • Swift - Unable to read/write from file
  • Save Image on iPhone idle time
  • Syntax error at const char *sql_stmt
  • Core Data Failing on saveToURL on UIManagedDocument
  • Delete all files and folder from a certain folder
  • Save Image on iPhone idle time
  • Move directory to trash
  • createDirectoryAtPath:attributes: alternative
  • NSFileManager URL vs Path
  • How to reduce the file size of a video recorded through my iOS app on a phone
  • Streaming wrapper around program that writes to multiple output files
  • Are there any cases where it's better to use [NSFileManager defaultManager] rather than [[NSFil
  • How do I count a number of images in a folder reference in swift
  • Audio file doesn't play in iPhone 5
  • DocumentsDirectory in UITableView Array for Xcode 7 and Swift 2.0
  • how to get all installed applications with Objective C in OSX
  • Batch to process files one by one
  • Post comment to WordPress Blog from iPhone programmatically
  • iOS Date formatting
  • Error while re-opening sound file in python
  • What is this iOS exception with NSCFType?
  • Less Conflicting Session Manager for Zope 2
  • how to solve invalid conversion specifier warning in iphone app
  • How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View?
  • Ubuntu and bcrypt
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Cassandra Data Model
  • Symfony2: How to get request parameter
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Compare two NSDates in iPhone
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Angular 2 constructor injection vs direct access
  • Can't mass-assign protected attributes when import data from csv file
  • How to get NHibernate ISession to cache entity not retrieved by primary key