All,
I am converting a project from Xcode 2.x (WO 5.3) to Eclipse 3.4 (WO 5.4.2). I have decided to also "upgrade" to using generics. So far, it has not been a fun experience. I've read over the threads on this list and several of the external references provided. Each time I think I have it figured out and try it on my code, I soon discover that I must be missing something. In many (but not all) cases, I was able to clear up the errors and warnings by making everything <?> or <? extends Object> but it just seems wrong to have to explicitly tell it to use whatever it wants. Below are a few examples of some typical code snippets that are causing problems. What is the correct way to write these using generics?
Based on further reading, I know this is not the right way to write this, but it works and does not generate errors/warnings.
NSDictionary<Object, Object> tmpDict = new NSDictionary<Object, Object>( new NSArray<Object>( new Object[] { location, schedType, Integer.valueOf( nWeekday) } ), new NSArray<Object>( new Object[] { "toLocation", "toScheduleType", "toDay.displayOrder" } ) );
NSArray<?> schedules = EOUtilities.objectsMatchingValues( ec, "Schedule", tmpDict );
---------
In theory, I believe this is the "more correct" way to write it but this generates warnings/errors
NSDictionary<String, ? extends Object> tmpDict = new NSDictionary<String, ? extends Object>( new NSArray<? extends Object>( new Object[] { location, schedType, Integer.valueOf( nWeekday ) } ), new NSArray<? extends Object>( new Object[] { "toLocation", "toScheduleType", "toDay.displayOrder" } ) );
NSArray<Schedule> schedules = EOUtilities.objectsMatchingValues( ec, "Schedule", tmpDict );
One problem with this approach is that, while the keys are, for all intents and purposes, Strings, you have to declare the array as <Object> because it is created with "new Object[] { entries }."
---------
Here, I get a warning icon in the gutter of the first line telling me "The _expression_ on type NSArray needs unchecked conversion to conform to NSArray<Schedule>." I'm sure this is because EOUtilities don't explicitly return an array of Schedules. But, if I change it to <?> or <? extends EOGenericRecord> or <? extends EOCustomRecord>, then the warning goes away and the next line generates an error icon and complains about the signature of showSchedules(). Again, when I alter that, it cascades like dominoes.
NSArray<Schedule> schedules = EOUtilities.objectsForEntityNamed( ec, "Schedule" ); showSchedules( schedules );
public static void showSchedules( NSArray<Schedule> schedules );
---------
availabilityByPeriods is a dictionary of dictionaries of strings { PERIOD = { "HHMM" = "1"; } } sKey = "MMDDYYYY_HHMM"
public static NSMutableDictionary<String, ? extends Object> arrangeTimeSlotsByPeriod( NSMutableDictionary<String, ? extends Object> availabilityByPeriods, String sKey ) { NSMutableDictionary<String, ? extends Object> dateDict = new NSMutableDictionary<String, Object>(); NSMutableDictionary<String, ? extends Object> periodDict = new NSMutableDictionary<String, Object>(); String sDate = sKey.substring( 0, 8 ); String sTime = sKey.substring( 9 ); int nPeriod = TimePeriod.timePeriodForKey( sKey );
if ( availabilityByPeriods.objectForKey( sDate ) == null ) availabilityByPeriods.setObjectForKey( dateDict, sDate );
// Type safety: Unchecked cast from capture#40-of ? extends Object to NSMutableDictionary<String, ? extends Object> dateDict = (NSMutableDictionary<String, ? extends Object>) availabilityByPeriods.objectForKey( sDate );
if ( dateDict.objectForKey( TimePeriod.dayPeriods.objectAtIndex( nPeriod ) ) == null ) dateDict.setObjectForKey( periodDict, TimePeriod.dayPeriods.objectAtIndex( nPeriod ) );
// Type safety: Unchecked cast from capture#45-of ? extends Object to NSMutableDictionary<String, ? extends Object> periodDict = (NSMutableDictionary<String, ? extends Object>) dateDict.objectForKey( TimePeriod.dayPeriods.objectAtIndex( nPeriod ) ); periodDict.setObjectForKey( "1", sTime ); // Value is unimportant return availabilityByPeriods; }
How can I find "capture #x of type _expression_" as referenced in the error messages?
---------
This one is even more confusing to me. It looks the same as some of the snippets above. Yet, in this case, it complains that "the constructor NSDictionary<String, Object>( NSArray<Object>, NSArray<Object> ) is undefined."
NSDictionary<String, ? extends Object> tmpDict = new NSDictionary<String, Object>( new NSArray<Object>( new Object[] { location, schedType, Integer.valueOf(nWeekday) } ), new NSArray<Object>( new Object[] { "toLocation", "toScheduleType", "toDay.displayOrder" } ) );
Roger
|