enum values passing as arguments in Objective-C - objective-c

i have an enum.
Example:
enum events
{
MOVE_UP = 0,
MOVE_DOWN,
MOVE_RIGHT,
MOVE_LEFT,
};
i want to pass particular enum value as function arguments. for example:
my method definition is
(void) invokeEvents:(enum events)events withMessage:(NSDictionary*)message;
while calling this method i am passing arguments like:
[self invokeEvents:MOVE_UP|MOVE_DOWN|MOVE_RIGHT withMessage:message;
while checking the received parameter events is value is always last value of list of MOVE_UP|MOVE_DOWN|MOVE_RIGHT values. MOVE_RIGHT 2 as per enum value.
but i want all the values like "MOVE_UP|MOVE_DOWN|MOVE_RIGHT" equal 0, 1 and 2.
how can i pass the parameter so that i can get all values.
kindly give suggestion for my problem.
Thanks in advance.

Change your enum to:
{
MOVE_NONE = 0
MOVE_UP = 1<<0,
MOVE_DOWN = 1<<1,
MOVE_RIGHT = 1<<2,
MOVE_LEFT = 1<<3,
};
So you can pass parameters exactly as you want: MOVE_UP|MOVE_DOWN|MOVE_RIGHT.
And in invokeEvents:withMessage: check
if(events & MOVE_UP)
{
}
if(events & MOVE_DOWN)
{
}
...
.

Related

Type in Array changed when Converting Objective-C Class to Swift

I post a request to get some json data and try to convert the data to Model
var newDrugs = Array<DxyDrugInfo>()
newDrugs = self.parseDrugJsonToModels(data) ?? []
private func parseDrugJsonToModels(data: NSData) -> Array<DxyDrugInfo>?{
let json = JSON(data: data)
if let drugsArray = json["data"].arrayObject as? Array<[String : AnyObject]>, success = json["success"].bool where success {
var sortedArray = Array<DxyDrugInfo>()
for drugDic in drugsArray {
let drugInfo = DxyDrugInfo()
drugInfo.setValuesForKeysWithDictionary(drugDic)
sortedArray.append(drugInfo)
}
return sortedArray
}
return nil
}
DxyDrugInfo is a Objective-C Model Class
My question is the sortedArray's type is Array of DxyDrugInfo, and it changed to
when assigned to newDrugs.
I want to convert it to DxyDrugInfo and I also want to what the type is.
Thank you
That type you're looking at, #lvalue [DxyDrugInfo], is "array of DxyDrugInfo". You're already done! The square brackets around a type are shorthand for "array", that is:
[DxyDrugInfo] == Array<DxyDrugInfo>
and #lvalue just means that newDrugs is a variable you can assign to. (It's the opposite of "r-value", which is a value you could assign to an "l-value".)

lua:How to use value from table 'A', in table 'B', which nested in table 'A'

In real project, TEST_TABLE would contain much of TEST_TABLE_NESTED, each with its own testVariable and bunch of testScript. test function from testScript would be used in C++ code, and TEST_TABLE_NESTED tables would be added automatically from C++ code too.
TEST_TABLE =
{
TEST_TABLE_NESTED =
{
testVariable = 5,
testScript =
{
test = function()
print(testVariable, "hello") --How to access 'testVariable'?
end
}
}
}
EDIT :
This is the actual scenario of using this script:
GameObjectScriptTables =
{
GameObject_1 = --Container of scripts corresponding to some gameObject
{
gameObjectOwner = actual_object_passed_from_c++, --This is an actual object passed from c++
GameObjectScript_1 = --This is a script with update(dt) method which will be called somwhere in c++ code
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
}
}
GameObject_2 =
{
gameObjectOwner = actual_object_passed_from_c++,
GameObjectScript_1 =
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
},
GameObjectScript_2 =
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
}
}
--And so on
}
Idea is that exists some testVariable object (passed from C++), which data is used all over TEST_TABLE_NESTED. For me, above example looks natural for this task, but it prints nil instead of 5. So how to acces a testVariable from testScript without printing a full path like TEST_TABLE.TEST_TABLE_NESTED.testVariable?
You're asking for something like a "parent" pointer, which tells table B about table A, but that doesn't exist. Internally, the only association they have is that one of A's values happens to be B, but any number of tables could contain B as a value. Which is B's parent?
If you want B to know about A, you'll need to tell it. You can add an extra parameter to update which receives the game owner object, or update can be a closure which contains the game owner as a bound variable, so on and so forth.
I made it work by providing a gameObjectOwner instance for each GameObjectScript_N. However I don't know is it expensive solution or not.

Convert IList<Interface> to List<Class>

List<CurrentElectionService> result = new List<CurrentElectionService>();
result = oElectionsManager.GetCurrentElectionsByEId(
employeeId.StringToGuid(), planYear) as List<CurrentElectionService>;
Public class CurrentElectionService : ICurentElection
{
// Implement Interface fields here
}
The method GetCurrentElectionsByEId returns me IList<ICurentElection> and I want to cast the interface into class CurrentElectionService, but it returns null. Please help.
Why not use LINQ to perform your cast
List<CurrentElectionService> result = oElectionsManager.GetCurrentElectionsByEId(
employeeId.StringToGuid(), planYear).Cast<CurrentElectionService>().ToList();
I hope this helps.
You need to find out of which type the actual return value is. The as keywort always returns null if your actual object is not of the type you want to cast it to.
With the definition you gave you could also try this:
List<ICurentElection> result;
result = oElectionsManager.GetCurrentElectionsByEId(
employeeId.StringToGuid(), planYear) as List<ICurentElection>;

Checking for a specific value in an ArrayList

I need to determine if a element in my ArrayList has a specific value. This is the current way I am trying.
public static boolean hasDwgNamedReference(Object valueDWG){
boolean b = false;
String dwgName = "dwg_";
if(valueDWG == null){
b = false;
}
else {
String checkNamedRef = Arrays.toString((Object[]) valueDWG).substring(1, 5);
System.out.println("checkNamedRef " + checkNamedRef + "\n");
if(checkNamedRef.equals(dwgName)){
b = true;
}
}
return b;
}// end hasDwgNamedReference
I am pretty sure that the issue is with the
String checkNamedRef = Arrays.toString((Object[]) valueDWG).substring(1, 5);
Do you think I need to increment the ArrayList and check each element?
Well, firstly, you do not need to include the first "if" statement. b is already false, so setting it to false again is unnecessary.
If you use the Arrays.toString() method on an object, you may get the identity of the object, or the address in memory that it holds. If you try to compare to checkNamedRef via an equals method, you may be comparing an address and a string, which would never give you the true you are looking for.

Cocoa Touch - int as string format

playerOneScore is a int, How can I pass it to use a label to display the score? This code just prints %i...
-(void)updateScoreLabels{
playerOneScoreLabel.text = #"%i",playerOneScore;
playerTwoScoreLabel.text = #"%i",playerTwoScore;
playerThreeScoreLabel.text = #"%i",playerThreeScore;
playerFourScoreLabel.text = #"%i",playerFourScore;
}
You need to initialize string using convenience constructor:
playerOneScoreLabel.text = [NSString stringWithFormat:#"%i",playerOneScore];
...
What you actually have in your code is comma operator - it evaluates its 1st parameter (that is assigns #"%i" string to a label), then evaluates and returns 2nd parameter - playerOneScore.