I am learning Objective-C and I am wondering when people say that when you out void in a method it means that method returns nothing. What does that exactly mean? What would it "return" if it wasn't void? Please help.
void is exactly what you said, its just a word you use to let the compiler know that this function doesnt return anything, and you may omit the return statement in the function eg
- (void) myMethod {
//do something here
}
where as if a method has a return type, then you would get a compile error if you didnt return something of that type
- (BOOL) myMethod {
BOOL x = false;
//do something
return x;
}
so here we can see, in your words "What would it "return" if it wasn't void?" that it would return whatever you tell it to, the return type when you declare the method just needs to match what you are actually returning and thats all really.
note you can still return in a void function, but all it does is prematurely end the functions execution, eg
- (void) myMethod {
BOOL x = false;
if(x == false)
return;
x = true;
}
here we can see that the statement x = true will never execute, because it will go into the if statement and return, which will end the function (and not return anything)
It would return whatever it has been declared/written to.
Related
Usually we will pass an object to #synchronized() block for unique reference. for example,
+(id)sharedDBHandler
{
#synchronized (self) {
if (sDBHandler == nil) {
sDBHandler = [self new];
}
}
return sDBHandler;
}
what happens if we pass nil to it?
It doesn't #synchronize() at all. No locks taken. No-op. Undefined behavior.
Perfectly valid question, btw, regardless of whether the code is antiquated and no longer the correct means of generating a singleton.
From the github repository. While not a documented claim, breaking this policy would cause compatibility hell.
int objc_sync_enter(id obj)
{
int result = OBJC_SYNC_SUCCESS;
if (obj) {
SyncData* data = id2data(obj, ACQUIRE);
assert(data);
data->mutex.lock();
} else {
// #synchronized(nil) does nothing
if (DebugNilSync) {
_objc_inform("NIL SYNC DEBUG: #synchronized(nil); set a breakpoint on objc_sync_nil to debug");
}
objc_sync_nil();
}
return result;
}
Where:
BREAKPOINT_FUNCTION(
void objc_sync_nil(void)
);
I have an Objective C function declared as:
+ (BOOL)isScanningUnsupported:(NSError **)error;
and if it returns true, I have to return nil in the function i am calling it from (Swift).
So I call if like this:
var isUnsupported = false
do { try isUnsupported = PPCoordinator.isScanningUnsupported()
} catch let error { throw error }
if isUnsupported {return nil }
But it tells me:
Cannot assign a value of type '()' to a value of type 'Bool'
In Objective C it was called like:
if ([PPCoordinator isScanningUnsupported:error]) {
return nil;
}
what can I do????
You are describing the standard way that functions that produce an error in Objective-C are interpreted in Swift. Methods in Objective-C that take a pointer to an error and return BOOL are assumed to be void and throw in Swift by default.
This is because that is almost always what it means in Objective-C. i.e. true means "ran successfully", false means "failed and I put the error in the pointer you supplied".
Your method doesn't conform to normal Objective-C conventions so you could try disabling Swift's conversion to a throwing function with the NS_SWIFT_NOTHROW macro on the declaration of PPCoordinator.isScanningUnsupported.
I think you want:
var isUnsupported = false
do { try PPCoordinator.isScanningUnsupported()
isUnsupported = true
} catch let error {
throw error // or do nothing?
}
if isUnsupported {return nil }
If you look at the Swift definition of your function, I would expect no return value (i.e. void).
this is working now for me:
if let _ = try? PPCoordinator.isScanningUnsupported() { return nil }
I know this is probably a simple queston, I would like to return the value of currentLocGeoPoint and return the array of Objects which is of type PFObject.
Tried to save it as a global variable, but it doesn't work because it is asynchronous and doesn't take a value yet. Returns empty.
Tried to return currentLocGeoPoint and changed Void in to PFGeoPoint in. Gives error: PFGeoPoint is not convertible to 'Void'
So I'm not sure how I can fetch the variable currentLocGeoPoint.
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
if (error != nil) {
println("Error:" + error.localizedDescription)
//return
}
if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
self.displayLocationInfo(pm)
currentLoc = manager.location
currentLocGeoPoint = PFGeoPoint(location:currentLoc)
var query = PFQuery(className:"Bar")
query.whereKey("BarLocation", nearGeoPoint:currentLocGeoPoint, withinMiles:10)
query.limit = 500
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]!, error: NSError!) -> Void in
if objects != nil {
} else {
println("error: \(error)")
}
}
} else {
println("error: \(error)")
}
})
}
I don't understand the notion of "I want to return currentLocGeoPoint". Return it to what? You're in a CLLocationManagerDelegate method, so there's no one to return it to.
What you could do, though, is, when the request is done (i.e. within this closure), call some other function that needed the currentLocGeoPoint. Or you could update the UI to reflect the updated information (make sure to dispatch that update to the main thread, though). Or, if you have other view controllers or model objects that need to know about the new data, you might post a notification, letting them know that there is an updated currentLocGeoPoint. But within this method, there's no one to whom you would "return" the data.
You could assign it to a stored property of your class. Just use
self.<property> = currentLocGeoPoint
This is really simple but driving me nuts.
I am trying to implement a simple function in my Objective-C code. When I write this,
NSInteger Sort_Function(id id1, id id2, void *context) {
}
I get an error that a semi-colon was expected at the end of the declaration. However, I've seen this type of syntax in many, many examples. What could I possibly be doing wrong? If it matters, this is an iOS app and the function is nested in an if clause. Thanks in advance.
The function definition -- this snippet you posted -- is "nested in a if clause"? That's unfortunately illegal in C (and Obj-C by extension) -- all function declarations and definitions have to be at the top level of the file. Inside an #implementation section is also an option:
// Start of file
// Declaration or definition valid here
void my_func(void); // Declaration
void my_other_func(void);
void my_third_func(void);
void my_func(void) { // Definition
return;
}
#implementation MyClass
// Definition also valid here
void my_other_func(void) {
return;
}
- (void) myMethod {
if( YES ){
void my_third_func(void) { // Invalid here
return;
}
}
}
#end
Is it possible you're confusing the function syntax with block syntax?
// The caret indicates definition of a block, sort of an anonymous function
int (^myBlock)(int);
myBlock = ^int (int my_int) {
return my_int;
};
How to get the properties of an interface within the type library the interface is defined, Keeps returning HRESULT but would like it to return the actual value of the property.
EDIT:
IDL:
interface IMyClassInterface : IDispatch
{
[propget, id(1), helpstring("Gets user Type")] HRESULT getUserDefineTypeVal([out,retval] UserDefineEnum *ptrVal);
[propput, id(1), helpstring("Sets user Type ")]HRESULT setUserDefineTypeVal([in] UserDefineEnum newVal);
}
Property in Header File:
STDMETHOD(getUserDefineTypeVal)(UserDefineEnum *ptrVal);
STDMETHOD(setUserDefineTypeVal)(UserDefineEnum newVal);
Property in MYClass.cpp:
STDMETHODIMP CMYClass::getUserDefineTypeVal(UserDefineEnum *ptrVal) {
*ptrVal = UserDefineEnum(private_var_UserDefineTypeVal);
return S_OK;
}
AnotherClass within the Type Library:
IMyClassInterface* private_var_MyClass
STDMETHODIMP CAnotherClass::someMethod(){
UserDefineEnum* p;
if(private_var_MyClass->getUserDefineTypeVal(p)){
//do somestuff
}
}
The problem is the if condition doesn’t return true. However the below partially works.
HRESULT hr = private_var_MyClass->getUserDefineTypeVal(p);
if(hr == S_OK){ do somestuff }
The problem with this is if I attempt a case statement the only value in hr is 0. I need to check the value being set on the clientside.
The value of S_OK is 0, that's why your if() statement doesn't execute. You should use the SUCCEEDED macro:
UserDefinedEnum value;
HRESULT hr = private_var_MyClass->getUserDefineTypeVal(&value);
if (SUCCEEDED(hr)) {
switch (value) {
// etc...
}
}
else {
// do something with the error...
}
COM usually uses out parameters to return values. In C/C++ you have to pass a pointer to a variable which will contain the result upon return.
The HRESULT return parameter is only used to report the success (or failure) of the method call.
EDIT For your code, you need to reserve the memory for the result by the caller:
UserDefineEnum p; // No * here ...
if (private_var_MyClass->getUserDefineTypeValue(&p) == S_OK) { // note '&' operator!
switch (p) {
case ENUM_1: // ...
case ENUM_2:
// ...
}
}