开发者

Java - Objective C - for each loop issue

开发者 https://www.devze.com 2023-04-01 16:06 出处:网络
I\'m trying to build an application in Android using some codes from Objective-C (IPhone app).I\'m trying to understand what exactly is doing a piece of code and translate it into Java code,but I thin

I'm trying to build an application in Android using some codes from Objective-C (IPhone app).I'm trying to understand what exactly is doing a piece of code and translate it into Java code,but I think I need a little help here.So first, here is the Obj-C code :

  (BOOL)processSqlInjectQueries:(NSArray *)injectQueries error:(NSError**)error {
    //some code

    for(NSDictionary * q in injectQueries)
                {
                    if (![q isKindOfClass:[NSDictionary class]]) continue;

                    StPacketInjectQueryPackage qType  = (StPacketInjectQueryPackage)[[q objectForKey:@"type"] intValue];
                    NSString * query = [q objectForKey:@"query"];
                }
    //some code
    }

In Java, I'm trying to do something like this :

         // in some other method :

        JSONObject jsonData= new JSONObject();
        String authHash = jsonData.getJSONObject("client_auth_hash").toString();
        List<Map<String,String>> injectQueries= new ArrayList<Map<String,String>>();
        injectQueries.add(new HashMap<String, String>());
        //injectQueries.add(authHash);


    public boolean processSqlInjectQueries(List<Map<String,String>> injectQueries, Exception error){

        if(injectQueries==null || injectQueries.size()==0){
            boolean injectsProcessed = this.processSqlInjectQueries(injectQueries,error);
            if(!injectsProcessed){
           开发者_StackOverflow社区     return false;
            }
        }

        Log.i("Info","Processing INJECT Queries...");
        boolean res = true;
        /*[_dbAdapter beginTransaction];
        [_user.userDbAdapter beginTransaction];*/

        for(Map<String,String> b : injectQueries){

            if(b.getClass().getName()!=injectQueries.getClass().getName()){
                continue;
            }
            //RPCPacketInjectQueryPackage qType = (RPCPacketInjectQueryPackage) 
        }

        return true;
    }

But my problem is that is that I get this error : Type mismatch: cannot convert from element type Object to ArrayList.

Any suggestion how to fix that error?

And second question : Can I use Exception error in declaraion of processSqlInjectQueries instead of NSError *error in Obj-C?


Iterating over the ArrayList

The error is being raised because your code needs to declare that b is an Object (since injectQueries contains objects of type Object, not objects of type ArrayList):

for(Object b : injectQueries){
    ...
}

Since the NSDictionary class in Objective-C closely resembles the Map class in Java, you can mimic the Objective-C code by casting b as a Map, or even better, you can use generics to specify that injectQueries contains Map objects. For example:

public boolean processSqlInjectQueries(List<Map<String,String> injectQueries, Exception error) {

    // some code

    for(Map<String,String> b : injectQueries) {
        ...
    }

    // some code
    return true;
}

Exception Handling

As for your second question, methods in Java normally communicate error conditions by throwing Exception objects, so your method signature would resemble the following:

public boolean processSqlInjectQueries(List<Map<String,String> injectQueries) throws Exception

Note that it's always better to be specific with your exceptions (i.e. to throw objects that are subclasses of Exception) so that your method caller has some idea of what went wrong. See the following link for additional guidelines for handling exceptions in Java:

http://www.javapractices.com/home/HomeAction.do#Exceptions


The objective C code is passing through an NSArray containing NSDictionaries through to the method, so the following is probably closer to what you want to do...

public boolean processSqlInjectQueries(List<Map<String,String> injectQueries) {
    for(Map<String,String> q : injectQueries) {
        // do stuff with q
    }
}

To handle the error code, you probably want to think about throwing an exception rather than trying to pass through an "Error" object, so something like the following:

public boolean processSqlInjectQueries(List<Map<String,String> injectQueries) throws Exception {
    for(Map<String,String> q : injectQueries) {
        // do stuff with q
    }

    if(errorConditionOccurs) {
       throw new Exception();
    }
}

You'll want to tailor the exact Exception that's thrown so it matches whatever your code is trying to do.

In response to comment: perhaps this is closer to what you're trying to do?

public boolean processSqlInjectQueries(JSONObject jsonObject) {
    for(String key : jsonObject.keys()) {
        Object value = jsonOnject.get(key);
        // Do stuff with value
    }
    // Do more stuff
}

And you could call it with:

processSqlInjectQueries(jsonData.getJSONObject("client_auth_hash"));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号