"void value not ignored as it ought to be" error - iphone

I am getting an error "void value not ignored as it ought to be" on the line:
home = [[Home alloc] initWithPhoto:imageView.image];
Please help.

Your Home class' -initWithPhoto: function is probably returning void. Initializer functions are supposed to return id.

What is the return value of initWithPhoto? The error means that the method is returning void but you are assigning that to something. initWithPhoto should look something like this:
- (id)initWithPhoto:(UIImage *)image {
if (self = [super init]) {
// do your tasks
}
return self;
}
The method should return id, not void.

Your initWithPhoto method is returning an object of type void when it should be returning an object of type id, especially if you're expecting the home variable to contain anything useful. Try editing the initWithPhoto method to return an object of type id.

Related

ReactiveCocoa observe two property

I need to check two values and set conditions based on these two values, return a NS_ENUM value.
From ReactiveCocoa github readme, I find this
RAC(self, createEnabled) = [RACSignal
combineLatest:#[ RACObserve(self, password), RACObserve(self, passwordConfirmation) ]
reduce:^(NSString *password, NSString *passwordConfirm) {
return #([passwordConfirm isEqualToString:password]);
}];
It check two value, the password and passwordConfirm together. I tried to modify it a bit to observe two BOOL property, it shows me "Incompatible block pointer types" error..
RAC(self, showButtonOption) = [RACSignal
combineLatest:#[ RACObserve(self, setting), RACObserve(self, billing) ]
reduce:^(NSSet *setting, NSSet *billing) {
if ([billing containsObject:kBillingExpired]) {
return DialerShowButtonPurchase;
} else if ([setting containsObject:kSettingEnableRecord]) {
return DialerShowButtonRecord;
} else {
return DialerShowButtonCall;
}
}];
I don't know what went wrong and what should be the right syntax to serve the purpose?
Well, let's see what the signature of that method is:
+ (RACSignal *)combineLatest:(id<NSFastEnumeration>)signals
reduce:(id ( ^ ) ( ))reduceBlock
You're trying to return an enum value, a primitive, from the reduceBlock -- which must have return type of id.
This is an annoying but sadly unavoidable aspect of ReactiveCocoa: you need to box. A lot. If you return #(DialerShowButtonPurchase) (etc), you'll actually be returning an NSNumber *, which is an id, so it'll compile.
The RAC macro will automatically unbox it so that showButtonOption doesn't need to be declared as an NSNumber *.

Function Downcasting in UnityScript

I actually posted this on the Unity forums, but none of my language-related questions ever seem to get answered there. So, let's say I have a function defined as so in Unity Script:
function GetSomething : SomeClass
{
return new SomeClass();
}
Where SomeClass is some class defined elsewhere. Now, I have a variable, theFunction, of type Function, and I want to make sure that it returns something, anything. So what I do is the following:
// theFunction is set to GetSomething somewhere else in the program.
var functionThatReturnsSomething = theFunction as function() : Object;
if (functionThatReturnsSomething != null)
//... call it and do stuff with the returned value.
Now unfortunately, in the above code functionThatReturnsSomething will be null. For it not to be null, I have to be more specific and cast to function() : SomeClass OR just override the function definition to return an Object as so:
function GetSomething : Object
{
return new SomeClass();
}
This is very annoying because its easy to forget to do :Object (especially since if you leave it out it will correctly infer it to be of return type SomeClass), and the result is not an error, but rather a very subtle bug since the cast fails. Is there any way to get the behavior I want, which is for it to properly downcast to function() : Object, the same way I can downcast normal objects?
The only thing i can think of is you forgot the parenthesis after the function name when you declare it. If i put everything into the following script, attach it to an object and run it then i get access to the function with no problems.
#pragma strict
import System.Collections.Generic;
function Start ()
{
// theFunction is set to GetSomething somewhere else in the program.
var functionThatReturnsSomething = GetSomething as function() : Object;
if (functionThatReturnsSomething != null)
{
//... call it and do stuff with the returned value.
Debug.Log(functionThatReturnsSomething);
}
else Debug.Log("null");
}
function GetSomething() : List.<int>
{
return new List.<int>();
}

Aspectj access parameter of the calling method and the return value of a called method

I have to put some status data into the context object. This has to be done in lot of places so I thought it would be nice to solve this with AOP.
The problem is that I don't know how to access the callers parameter and the return value of the called method in the same advice. Is this possible at all?
public BusinessObject methodA(Context ctx, Param p) {
Status s = service.serviceMethod(p);
if (s.hasErrors())
{
ctx.put("error_key", s.getErrors());
}
...
}

Check for value in NSMutableArray

I have an NSMutableArray in my class containing Ingredient objects. I want to check whether the name property of any of the ingredients matches a string, but I can't get the syntax quite right.
I'm really missing Linq and predictates.
-(BOOL) hasIngredient:(NSString *)ingredientName{
for (Ingredient *ingredient in ingredients) {
//if([ingredient isKindOfClass:[Ingredient class]]){
if ([ingredient->name isEqualToString:ingredientName]) {
return YES;
}
//}
}
return NO;
}
The foo->bar syntax directly accesses instance variables. You shouldn't do that. The syntax to access a property is:
object.property
or:
[object property]
Accessing a property is always a method call. If you have a property foo and do #synthesize foo;, the compiler generates a method named foo and setFoo: (if the property isn't readonly).
So you should have something like:
#property(nonatomic,readonly) NSString *name;
Replace readonly with copy if you want the name to be changeable (the reason to use copy instead of retain is because you could pass a mutable string and then later modify that mutable string, which would sure yield unexpected results; you avoid that by copying).
Now your method becomes:
-(BOOL) hasIngredient:(NSString *)ingredientName{
for (Ingredient *ingredient in ingredients) {
if ([[ingredient name] isEqual:ingredientName]) {
return YES;
}
}
return NO;
}
Instead of [ingredient name] you can also write ingredient.name here, but I personally like the former better since the later is also used for accessing members of a struct which is "cheap" whereas accessing a property always involves a method call and thus is "more expensive". But that's just a matter of taste.
Change
if ([ingredient->name isEqualToString:ingredientName])
to
if ([ingredient.name isEqualToString:ingredientName])

Objective-C performSelector does not call a method with parameter

I'm having a problem with performSelector. It works if I call a method without parameters, but it doesn't even call the method if I pass a parameter.
Example:
- (void)test
{
NSLog(#"test"); //it works!!
}
...
[self performSelector:#selector(test) withObject:nil afterDelay:1.0];
- (void)switchOn:(NSNumber *) index
{
NSLog(#"switchOn"); //it doesn't work :-(
}
....
NSLog(#"int is %d", [((NSNumber *)obj) intValue]); //print the correct value
[self performSelector:#selector(switchOn:) withObject:obj afterDelay:1.0];
I get no errors neither. Where could it be the problem?
thanks
What is the type of the parameter for the switchOn: selector?
It must be of type id otherwise performSelector:WithObject: won't work. To quote the docs:
aSelector should identify a method that takes a single argument of type id. For methods with other argument types and return values, use NSInvocation.
performSelectorWithObject: sends a message to the selector with the object you supplied as the first argument. The receiving method must accept a single parameter of type id. For anything else use NSInvocation.
You might want to check out a similar question about this.
Try to use:
- (void)switchOn:(id)index