Diffence between two syntax for NSMutableDictionary - arrays

Can anybody please tell me the exact difference between these two syntax ,
I found it by co-incidence
Structre is like this :
-> NSMutableArray (CategoryAry)
...> NSMutableDictionary (multiple number of dictionaries)
NSLog(#"%#",[(NSMutableDictionary *)[CategoryAry objectAtIndex:indexPath.row+1] valueForKey:#"status"]);
NSLog(#"%#",(NSMutableDictionary *)[[CategoryAry objectAtIndex:indexPath.row+1] valueForKey:#"status"]);
Although both print the same results .

[(NSMutableDictionary *)[CategoryAry objectAtIndex:indexPath.row+1] valueForKey:#"status"]);
the "objectAtIndex" is casted to an NSMutableDictionary*, then a value "status" is searched inside it.
(NSMutableDictionary *)[[CategoryAry objectAtIndex:indexPath.row+1] valueForKey:#"status"]);
The "status" object is casted to an NSMutableDictionary*
The first call is just the right syntax.
For the second one, why does it works ?
valueForKey method is called on an id, and as it seems to be a dictionary, it works and returns a comprehensive result. Then on that result, the "description" method is called (#"%#"), and as that method exists on any object, casting the result to a NSMutableDictionary does not bug. The method is called on the subclass returned, and that return is displayed into the NSLog.
I guess that for the second call, you may have a compiler-warning ?

Related

$filter not finding result because of data type

In JSON i have data like
"phaseId":1234
and i have written code like
$filter('filter')(data, {phaseId: phaseId.toString()},true) this finds results.
but below code does not find. Is there any specific reason for this?
$filter('filter')(data, {phaseId: phaseId},true)
1234 is of type number and phaseId passed is also number
Remove the last parameter true (which means strict comparison, will assert that type and value are equals):
$filter('filter')(data, {phaseId: phaseId});

Converting an object into array/matrix?

I have an object in Matlab created from a third party toolbox. Within the object is a 3x65 double array. If I type the name of the object in the Matlab console, it lists all the contents, and specifically says this 3x65 array is a double. All I want to do is to extract this array into a separate Matlab array. But when I do something like:
x = object.ArrayIWant
I get the error "Access to an object's fields is only permitted within its methods." If I try the following:
x = get(object,'ArrayIWant)
I get the error "Conversion to double from 'toolboxfunction' is not possible. How do get access to this array?!
Look for "Get" methods in the class:
methods(object)
or
methods className
Say it says there is a method called GetArrayIWant, then you'd do:
x = object.GetArrayIWant();

Error with ibatis sql iterate

I'm using ibatis + DWR , but when i pass a map to ibatis i will get an error as below:
Cause: com.ibatis.sqlmap.client.SqlMapException: ParameterObject or
property was not a Collection, Array or Iterator.
here is my sql:
<update id="updateDarenFlagByUserTagIDs" parameterClass="java.util.Map">
update system_usertag
set isdaren = 1
where uid = #uid#
<isNotEmpty prepend=" AND " property="utidlist">
and utid in
<iterate open="(" close=")" conjunction="," property="utidlist">
#utidlist[]#
</iterate>
</isNotEmpty>
</update>
and here in the DWR part, i passed a map as below:
{'uid':uid, 'utidlist':utidlist}
Any ideas on this error?
I have answered the exact same question in the following post https://stackoverflow.com/questions/18997883/malformed-url-exception-in-ibatis/19025819#19025819 do make reference to it. The solution to your problem is very simple, ensure that the argument nested in your iterate tag <iterate></iterate> which in your case is "utidlist" is indeed a list/collection and nothing but a list. The cause of this error is that your iterable property "utidlist" isn't a list. If you can get it to be a list and not a flat value you'll just be fine.
Just in case you can still get it to work you may also want to paste a full stack of your logs so that you can see what is going wrong.

can't convert Enumerator into Array

While working on one application I am getting this error:
can't convert Enumerator into Array
Here is my code, mr_collection is MongoID query.
mr_collection = self.where(query).map_reduce(map, reduce).finalize(finalize).out({:replace => 'mr_results'})
paginator = WillPaginate::Collection.new(page, limit, collection_count)
collection = mr_collection.find(
:sort => sort,
:limit => limit,
:skip => skip
)
paginator.replace(collection)
While getting mr_collection, if I inspect the result mr_collection gives me:
[
{"_id"=>1.0, "value"=>{"s"=>4.2, "p"=>14.95, "pml"=>0.01993}},
{"_id"=>2.0, "value"=>{"s"=>3.7, "p"=>12.9, "pml"=>0.0172}},
{"_id"=>3.0, "value"=>{"s"=>4.2, "p"=>12.9, "pml"=>0.0172}},
{"_id"=>4.0, "value"=>{"s"=>4.0, "p"=>11.95, "pml"=>0.01593}},
{"_id"=>300.0, "value"=>{"s"=>0.0, "p"=>8.95, "pml"=>0.01193}},
]
While getting collection, if I inspect the result collection gives me:
#<Enumerator: []:find({:sort=>[["value.s", :desc], ["value.pml", :asc]], :limit=>10, :skip=>0})>
I am getting error on the line paginator.replace(collection). I'm using Ruby 1.9.3 & Rails 3.2.6.
collection is an Enumerator which obviously can't convert into an Array, which is what replace expects.
Here are the comments from the rubydocs:
Enumerable#find(ifnone = nil) { |e| ... }
Passes each entry in enum to block. Returns the first for which block
is not false. If no object matches, calls ifnone and returns its
result when it is specified, or returns nil otherwise.
If no block is given, an enumerator is returned instead.
Therefore you have two options:
If you want all elements, yield from the Enumerator to an Array.
If you only want the first match, supply a block that determines what the match is.
Hope this helps.
(Moral of the story: always read the docs!)
I have no idea about mongoid having never used it.
But a search has brought to fore an awfully similar question -
Mongoid 3 - access map_reduce results
Unfortunately my environent is not set to test the magic of
collection = mr_collection.send(:documents).sort(sort).limit(limit).skip(skip).to_a
Have you had a look at this link? Hopefully it'll help solve your issue!

why does gql query result cause exception when calling get()

I am attempting to retrieve an entry from my datastore with this:
query = UserData.gql("WHERE mDeviceId = :1", id)
Utils.log("my Object:" + str(query))
entry = query.get()
It just so happens that the variable 'id' doesn't even exist (typo), so I know how to fix this, but I don't understand why the result I get won't let me call get() on it. When I do, I get the error:
Exception: Unsupported type for property : <type 'builtin_function_or_method'>
Normally I just check if entry == None to see if I get no results. Does anyone know why this occurs and if I should be doing my checks for None differently, in case I have such typos in the future?
A variable named id is not defined in your code, so it passes the builtin function id, and QL complains that it's getting a function when it expected a value (integer?).
Check to make sure you're assigning id a value before you use it.
Even better, don't shadow builtins with your own variables-- it'll cause confusing errors like this. :-)

Resources