typecasting before findByPk ?

9 views
Skip to first unread message

yohann

unread,
Mar 28, 2009, 9:59:14 AM3/28/09
to sqlitepersistentobjects-user
Hi persistent users,

I've been very hapy so far with sqlitepo and want to thanks everybody
who contributed.

I hope you can give me a help with this problem I have. I'm new to
objective-c (Ruby background) so maybe I'm missing something obvious.

I've read the readm.txt and the presentation Jeff made. But when I do
this is my code :

Person* person = [Person findByPk:1];

I get the following error :'warning assignment from disctinct
objective-c type'

I've checked the returned object cast of [Person findByPk:1] and
without typecasting it with (person*), it returns correctly a Person
object.

When I do NSArray* array = [Person allObjects] I don't have this type
warning.

In my header file, I have :

@class Person (for when I need to alloc)

in my implementation file :

#import 'Person.h'

So I'm not sure how I get this warning..but I don't like to typecast
each time I use a method, especially as no sqlitepo tutorial I've read
made it like that.

Any of you could give me a hint of what I'm doing wrong or what I
actually missed ?

Thank you

Adam Jack

unread,
Mar 28, 2009, 3:31:35 PM3/28/09
to sqlitepersiste...@googlegroups.com
My guess is you need to accept the typecast (that is what I've been doing.) I guess the samples could just ignore the warning, but it is odd they don't cast.

If you really don't want to cast I guess you could create you own class method to wrapper this function, but I'd just cast.

The reason you don't need to typecast allObject is 'cos it returns an NSArray (of typed objects) and although the types change, the array is always an array.

regards

Adam

Adam Jack

unread,
Mar 28, 2009, 5:30:31 PM3/28/09
to sqlitepersiste...@googlegroups.com
I was reading this:


and came upon this:

The bottom two examples will generate a compiler warning 
because the compiler won’t be able to find the method. That’s logical, since 
it doesn’t exist - these methods are created dynamically at runtime. You 
can squelch the warning by creating a category on your own class in your 
class’ header file

@interface Person (squelch) 
+ (id)findByName:(NSString *)theName; 
    @end 

I wonder if something like that could help you squelch the type warnings.

BTW: Where are you finding sample code? I want to have an object have an array of sub-objects, but seem to have problems doing that & could use pointers.

regards

Adam

On Mar 28, 2009, at 7:59 AM, yohann wrote:

yohann

unread,
Mar 28, 2009, 10:35:43 PM3/28/09
to sqlitepersistentobjects-user
Thanks Adam for your answer,

I'm ok with typecasting to disable the warnings but it's still a bit
frustrating as it is no complex nested method call which different
return types caling one another.

I didn't find good tutorials except example code from the
presentation, so I'm not sure how other users dealt with Typecast.all
other turorials out there are short and didn't get into those details.

Still, findByPk is not dynamically generated and is defined in source
classes with finFirstByCriteria, findByCriteria...Ect. So my guess was
that it shouldn't bring warning issues. You're right also, the
interface tip is very handy for those dynamyc methods.

So if everybody typecasted so to get rid of the warnings,I guess I
would have to keep on doing that as well ?

Regards

(Person*)Yohann

Andrew

unread,
Mar 29, 2009, 12:21:05 PM3/29/09
to sqlitepersistentobjects-user


On Mar 28, 8:59 am, yohann <fil...@mac.com> wrote:

> Person* person = [Person findByPk:1];
>
> I get the following error :'warning assignment from disctinct
> objective-c type'
>
> I've checked the returned object cast of [Person findByPk:1] and
> without typecasting it with (person*), it returns correctly a Person
> object.
>

Yohann,


You could either do the cast or subclass the class method:

+ (Person *) findByPk: (int) inPk {

return (Person *)[super inPk];

} // findByPk


I, myself, prefer using the subclass. A regular use of casting is
showing a failure in your type system.


Anon,
Andrew

yohann

unread,
Mar 29, 2009, 4:23:02 PM3/29/09
to sqlitepersistentobjects-user

> You could either do the cast or subclass the class method:
>
> + (Person *) findByPk: (int) inPk {
>
>         return (Person *)[super inPk];
>
> } // findByPk
>
> I, myself, prefer using the subclass. A regular use of casting is
> showing a failure in your type system.
>

Thanks Andrew, it will shorten up my code a bit that way. I wish it
would have been designed in the source code but we're programmers and
we love writing code, don't we ? ;-)

thanks again and best regards

Yohann

David Sinclair

unread,
Mar 29, 2009, 4:41:55 PM3/29/09
to sqlitepersiste...@googlegroups.com
I actually filed a bug about this a few weeks ago. +findByPk: and
+findFirstByCriteria: should both return id rather than
SQLitePersistentObject to avoid the need for coercion.

<http://code.google.com/p/sqlitepersistentobjects/issues/detail?id=52>

--

David Sinclair, Dejal Systems, LLC - d...@dejal.com
Dejal blog - http://www.dejal.com/blog/
Cocoa code - http://www.dejal.com/developer/
Twitter - http://twitter.com/dejal/


Reply all
Reply to author
Forward
0 new messages