Swift: "Mapper is not a type", using custom mapping class for arrays in swift and Objective C - objective-c

I have an 2 objective C classes:
Class 1 scanDatabase (Scans a database and puts that into a mutable array)
Class 2 Mapper (Is a mapping class for the database scan "model")
In objective C this successfully scans the database and puts it into a mutable array. Using the mapping class I can access individual groups of elements (AlbumTitles) like so:
for (Mapper *mapper in scanResult) {
NSLog(#"%#", mapper.AlbumTitle);
}
Everything is working as it should and I can return individual elements from my array i.e as above I am only returning album titles.
I then need to use that array in Swift. I call the objective C in my Swift class and again it runs fine and creates the array. This is done with:
let scanTable = ScanTable();
let scanMapper = Mapper();
scanTable.scanTableDo();
but when I try to retrieve a particular set of items like Album title as I did in the objective C for loop above I get the error "scanMapper is not a type" (scanMapper is my swift instance of the objective C mapper class:
I tried two different ways and both have the same error:
for mapper: scanMapper in scanTable.scanResult {
print("\(mapper.AlbumTitle)")
}
for object in scanTable.scanResult as! [scanMapper] {
print("\(mapper.AlbumTitle)")
}
Can I use an objective C class as a model/mapper and not sure whether I would need to recreate it in Swift.
I will include the mapper and scanTable .h and .m code just in case it is needed, plus the bridging header:
Mapper.h:
#import <Foundation/Foundation.h>
#import <AWSDynamoDB/AWSDynamoDB.h>
#interface Mapper : AWSDynamoDBObjectModel <AWSDynamoDBModeling>
#property (nonatomic, strong) NSNumber *SongID;
#property (nonatomic, strong) NSString *Artist;
#property (nonatomic, strong) NSString *SongURL;
#property (nonatomic, strong) NSString *Location;
#property (nonatomic, strong) NSNumber *UserRatings;
#property (nonatomic, strong) NSNumber *AVGUserRating;
#property (nonatomic, strong) NSString *Category;
#property (nonatomic, strong) NSString *PictureURL;
#property (nonatomic, strong) NSNumber *SongDuration;
#property (nonatomic, strong) NSString *SongTitle;
#property (nonatomic, strong) NSNumber *AVGMusicianRating;
#property (nonatomic, strong) NSString *AlbumTitle;
#end
Mapper.m
#import <AWSDynamoDB/AWSDynamoDB.h>
#import "Mapper.h"
#implementation Mapper
+ (NSString *)dynamoDBTableName {
return #"Songs";
}
+ (NSString *)hashKeyAttribute {
return #"SongID";
}
#end
ScanTable.h:
#import <Foundation/Foundation.h>
#import <AWSDynamoDB/AWSDynamoDB.h>
#interface ScanTable : NSObject
- (void) scanTableDo;
#property (nonatomic, strong) NSMutableArray *scanResult;
#end
ScanTable.m
#import "ScanTable.h"
#import "Mapper.h"
#implementation ScanTable
- (void) scanTableDo {
AWSDynamoDBObjectMapper *dynamoDBObjectMapper = [AWSDynamoDBObjectMapper defaultDynamoDBObjectMapper];
AWSDynamoDBScanExpression *scanExpression = [AWSDynamoDBScanExpression new];
scanExpression.limit = #10;
[[dynamoDBObjectMapper scan:[Mapper class]
expression:scanExpression]
continueWithBlock:^id(AWSTask *task) {
if (task.error) {
NSLog(#"The request failed. Error: [%#]", task.error);
}
if (task.exception) {
NSLog(#"The request failed. Exception: [%#]", task.exception);
}
if (task.result) {
AWSDynamoDBPaginatedOutput *paginatedOutput = task.result;
NSMutableArray *scanResult = [[NSMutableArray alloc] initWithArray:paginatedOutput.items]; //// ADDED /////
for (Mapper *mapper in scanResult) {
NSLog(#"%#", mapper.AlbumTitle);
}
}
return nil;
}];
}
#end
//EDITED ADDED BRIDGING HEADER//
MySampleApp-Bridging-Header.h:
//
// MySampleApp-Bridging-Header.h
// MySampleApp
#import "ScanTable.h"
#import "Mapper.h"
#import "Hello World.h"
Thanks for your help

The problem is just as the error explains, you're attempting to cast the items in your array to scanMapper, which is a variable holding an instance of Mapper, not the Mapper type itself. Assuming that scanTable.scanResult is an NSArray of Mappers, try this instead:
guard let scanResult = scanTable.scanResult as? [Mapper] else {
print("scanResult was not an array of mappers!")
return
}
for mapper: Mapper in scanResult {
print("\(mapper.AlbumTitle)")
}

Related

Could not cast value of type - Objective C NSManagedObject in Swift

I've found numerous posts about this error being able to resolve by adding the prefix of the project name to the class in entity viewer e.g ProjectName.Journey also the suggestion of adding #objc(ClassName) above the class declaration. But these solutions don't work for me. The problem is I have a NSManagedObject class generated in Objective C that I then try to access in Swift. I get the following error:
Unable to load class named 'ProjectName.Journey' for entity 'Journey'. Class not found, using default NSManagedObject instead.
Could not cast value of type 'NSManagedObject_Journey_' (0x1700541f0) to 'Journey' (0x100096f38).
My Class:
H:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Journey : NSManagedObject
#property (nonatomic, retain) NSString * travelMode;
#property (nonatomic, retain) NSDate * startTime;
#property (nonatomic, retain) NSDate * endTime;
#property (nonatomic, retain) NSNumber * averageSpeed;
#property (nonatomic, retain) NSNumber * distance;
#end
M:
#import "Journey.h"
#implementation Journey
#dynamic travelMode;
#dynamic startTime;
#dynamic endTime;
#dynamic averageSpeed;
#dynamic distance;
#end
My code trying to access this code in Swift:
for res in results {
var move: Journey = res as! Journey; //error thrown here
var morningStamp = midnightDate!.timeIntervalSince1970;
var eveningStamp = morningStamp + 86400;
if(move.startTime.timeIntervalSince1970 > morningStamp) {
if(move.endTime.timeIntervalSince1970 < eveningStamp) {
filtered.addObject(move);
}
}
}
bridge header file:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "TravelRecordManager.h"
#import "Journey.h"

NSObject returns nothing XCode 6

I moved my app to XCode 6 and found this problem. I have NSObject and it stopped returning objects when initialized, I use XCode 6 iPhone 6 Simulator.
My .h file:
#import <Foundation/Foundation.h>
#interface RBGAlpha : NSObject{
NSString *red;
NSString *blue;
NSString *green;
NSString *alpha;
}
#property (nonatomic, retain) NSString *red;
#property (nonatomic, retain) NSString *blue;
#property (nonatomic, retain) NSString *green;
#property (nonatomic, retain) NSString *alpha;
-(id)initWithName:(NSString *)r bl:(NSString *)b gr:(NSString *)g al:(NSString *)a;
#end
my .m file
#import "RBGAlpha.h"
#implementation RBGAlpha
#synthesize red,blue,green,alpha;
-(id)initWithName:(NSString *)r bl:(NSString *)b gr:(NSString *)g al:(NSString *)a{
self = [super init];
if (self) {
self.red = r;
self.blue = b;
self.green = g;
self.alpha = a;
}
return self;
}
#end
I use something like this in viewDidLoad method to create my objects:
RBGAlpha *tmpObj=[[RBGAlpha alloc] initWithName:#"0.01" bl:#"0.01" gr:#"0.01" al:#"1.00"];
However, while running the app in Simulator iPhone 6 this returns nothing
Has anybody dealt with that kind of problem?
I think that you're being mislead. There is indeed a value, that is what 0x786... in the value field means.
Summary saying 0 objects is confusing. I cannot understand why it would say that, but I bet if you typed po tmpObj into LLDB it would not return nil but the address showing next to "Value".
If you want to see something more interesting from the Xcode debugger consider implementing debugQuickLookObject.
On a side note, you can omit the definition of your instances variables in
#interface RBGAlpha : NSObject{
NSString *red;
NSString *blue;
NSString *green;
NSString *alpha;
}
And you also don't need to #synthesize each of them anymore, the compiler included with Xcode 5 and up does this for you.

Why is my NSDictionary not initializing my specific keys and values when in my model, but works within my controller?

In this example my NSDictionary initializes with 0 key/value pairs, as shown in my debugger. It will initialize properly when I do the exact same thing in my ViewController but I would much prefer to stick to MVC design and have the NSDictionary in my model.
ShakespeareViewController.h
#import <UIKit/UIKit.h>
#interface ShakespeareViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *sonnetDisplay;
#end
ShakespeareViewController.m
#import "ShakespeareViewController.h"
#import "ShakespeareModel.h"
#interface ShakespeareViewController ()
#property (nonatomic, strong) ShakespeareModel *sonnet;
#end
#implementation ShakespeareViewController
#synthesize sonnetDisplay = _sonnetDisplay;
#synthesize sonnet = _sonnet;
- (IBAction)sonnetButton:(UIButton *)sender
{
self.sonnetDisplay.text = [self.sonnet grabSonnet:#"19"];
}
#end
ShakespeareModel.h
#import <Foundation/Foundation.h>
#interface ShakespeareModel : NSObject
-(NSString *)grabSonnet:(NSString *)atNumber;
#end
ShakespeareModel.m
#import "ShakespeareModel.h"
#interface ShakespeareModel()
#property (nonatomic, strong) NSDictionary *sonnets;
#end
#implementation ShakespeareModel
#synthesize sonnets = _sonnets;
-(NSDictionary *)sonnets
{
if (!_sonnets)
{
_sonnets = [[NSDictionary alloc] initWithObjectsAndKeys:#"19", #"19", nil];
}
return _sonnets;
}
-(NSString *)grabSonnet:(NSString *)atNumber
{
NSString *chosenSonnet = [self.sonnets objectForKey:#"19"];
return chosenSonnet;
}
#end
Any ideas on what I am doing wrong are greatly appreciated. I can't see why this wouldn't initialize with the object 19 at key value 19.
I don't see any place where you set the view controller's sonnet property to an instance of ShakespeareModel -- so when you call grabSonnet: you're sending a message to nil (and thus getting nothing back.
You should put self.sonnet = [[ShakespeareModel alloc] init] in your view controller some time before you call grabSonnet:... probably in the initializer or in -viewDidLoad.

How to access the object

I would like to access the the _news items in a loop. But don't know how to get this done.
My Game.m looks like
#import "Game.h"
#implementation Game
#synthesize homename = _homename;
#synthesize guestname = _guestname;
#synthesize date = _date;
#synthesize gametype = _gametype;
#synthesize news = _news;
#synthesize gameId = _gameId;
-(NSString*)description{
return [NSString stringWithFormat:#"%# gegen %# (%#)", self.homename, self.guestname, self.gametype];
}
#end
My Game.h looks like
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#import "News.h"
#interface Game : NSObject {
NSString* _homename;
NSString* _guestname;
NSString* _date;
NSString* _gametype;
NSNumber* _gameId;
// News* news;
#public
NSArray* _news;
}
#property (nonatomic, retain) NSString* homename;
#property (nonatomic, retain) NSString* guestname;
#property (nonatomic, retain) NSString* date;
#property (nonatomic, retain) NSString* gametype;
//#property (nonatomic, retain) News* news;
#property (nonatomic, retain) NSArray* news;
#property (nonatomic, retain) NSNumber* gameId;
#end
My News.h looks like
#import <Foundation/Foundation.h>
#import <RestKit/RestKit.h>
#interface News : NSObject {
NSString* _minute;
NSString* _title;
NSString* _bodytext;
NSString* _player;
}
#property (nonatomic, retain) NSString* minute;
#property (nonatomic, retain) NSString* title;
#property (nonatomic, retain) NSString* bodytext;
#property (nonatomic, retain) NSString* player;
#end
My news.m looks like
#import "News.h"
#implementation News
#synthesize player = _player;
#synthesize title = _title;
#synthesize bodytext = _bodytext;
#synthesize minute = _minute;
#end
And the code where I want to access the variable looks like:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(#"Loaded statuses: %#", objects);
HeaderText.text = [NSString stringWithFormat:#"%#", objects ];
// for(News* n in _news) NSLog([n _bodytext]);
// for (id object in objects) {
// NSLog(#"News = %#", object );
// }
}
The NSLog with objects looks good for the game. The next thing is that I want to do something like (above is more pseudo code than real code because I don't know how to do it right):
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(#"Loaded statuses: %#", objects);
HeaderText.text = [NSString stringWithFormat:#"%#", objects ];
// loop all
for (id object in objects) {
news = objects.news;
for (id mynews in news) {
NSLog(#"News minute = %#", news.minute );
NSLog(#"News title = %#", news.title );
NSLog(#"News bodytext = %#", news.bodytext );
NSLog(#"News player = %#", news.player );
}
}
}
How to do the getter/setter methods right (so that I can use it here)?
Sorry for the surely stupid question but I don't get the point with it.
Two things:
1stly, is the _news object a private variable, without having a property declaration (getters and setters for e.g.)? The '_variableName' format is usually used to denote private variables.
2ndly, if it is not a private variable, do all the items within the _news array belong to the same class?
If so, you can do a
for (NewsObject *theNewsObject in _news)
{
//code here
}
The for(id randomObject in array) is useful when you don't know what type of object is in the array or if the objects contained in the array are of different types.
Now, again, all the objects inside the NewsObject ought to be public properties that can be accessed by other classes (they should have getters and setters).
Hope this helps. :)
EDIT FOR UPDATED QUESTION
So, if I'm getting your question correctly, you have a Game object, which has an array of News object inside it.
So, in your Game.h
NSString* _homename;
NSString* _guestname;
NSString* _date;
NSString* _gametype;
NSNumber* _gameId;
NSArray * _newsObjects; //declare it as NSMutableArray if you need to mutate it
Now, where you declare your properties;
#property(nonatomic, retain)NSArray *newsObjects
Synthesize it like you normally would in the Game.m file
You are creating the getters/setters automatically by using the #synthesize directive. It creates the getters and setters for you.
Again, from your code, it looks like the NSArray of objects that are passed through the method
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
consist of Game objects.
So, to access the News object from within the array of Game objects, import News.h and Game.h in the class where this method is being executed, and do the following:
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
NSLog(#"Loaded statuses: %#", objects);
// loop all
for (Game *gameObject in objects) {
NSArray *newsObjectArray = [gameObject newsObjects] //or gameObject.newsObject
for (News *mynews in newsObjectArray) {
NSLog(#"News minute = %#", mynews.minute );
NSLog(#"News title = %#", mynews.title );
NSLog(#"News bodytext = %#", mynews.bodytext );
NSLog(#"News player = %#", mynews.player );
}
}
}
What I found in your code that there was code that was being executed which was not declared in any part of the example that you posted.
What the code above will do is it will look through the Game object array (called objects).
Then, for each gameObject within that array, it will loop through the array of newsObject for that gameObject.
Hope this helps. :)

iOS – NSMutableArray and Block Copy

Currently I’m trying to get the hang of a block copy with my current project. The structure of the copy is an NSMutableArray that contains NSIntegers, NSStrings another NSMutableArray and two Objects… Those objects in turn hold NSStrings. The Array contains Objects which hold an NSInteger and Two Objects which contain strings…
I believe I am supposed to use the Block Copy method for coping objects… Code is below…
I am aware the code is not releasing properly… I tried to make the code smaller for your benefit.
Any insight you could shed would be awesome.
//Main controller Excerpt
//Insert Position Information into temporary node point... Node Points can have multiple Positions (or rather you can face multiple directions at the node. Each Node has 3-4 of these.
[newNode.positionArray insertObject:[newPosition copy] atIndex:currentPosition];
Insert the temporary node into the Node Array.
[nodeArray insertObject:[newNode copy] atIndex:count];
//Main Controller Excerpt
//
// Node.h
//
#import <Foundation/Foundation.h>
#class Sequence;
#class Position;
#interface Node : NSObject {
NSInteger Id;
NSInteger currentPosition;
NSString *title;
NSMutableArray *positionArray;
Sequence *forwardSequence;
Sequence *backSequence;
}
-(id) copyWithZone: (NSZone *) zone;
#property (nonatomic, assign) NSInteger Id;
#property (nonatomic, assign) NSInteger currentPosition;
#property (nonatomic, assign) NSString *title;
#property (nonatomic, retain) NSMutableArray *positionArray;
#property (nonatomic, retain) Sequence *forwardSequence;
#property (nonatomic, retain) Sequence *backSequence;
#end
//
// Node.m
//
#import "Sequence.h"
#import "Position.h"
#import "Node.h"
#implementation Node
#synthesize Id;
#synthesize currentPosition;
#synthesize positionArray;
#synthesize title;
#synthesize forwardSequence;
#synthesize backSequence;
-(id) copyWithZone: (NSZone *) zone {
Node *nodeCopy = [[Node allocWithZone: zone] init];
nodeCopy.Id = Id;
nodeCopy.currentPosition = currentPosition;
nodeCopy.positionArray = [positionArray copy];
nodeCopy.title = title;
nodeCopy.forwardSequence = [forwardSequence copy];
nodeCopy.backSequence = [backSequence copy];
return nodeCopy;
}
#end
//
// Position.h
//
#import <Foundation/Foundation.h>
#class Sequence;
#interface Position : NSObject <NSCopying> {
NSInteger Id;
Sequence *leftSequence;
Sequence *rightSequence;
}
#property (nonatomic, assign) NSInteger Id;
#property (nonatomic, retain) Sequence *leftSequence;
#property (nonatomic, retain) Sequence *rightSequence;
-(id) copyWithZone: (NSZone *) zone;
#end
//
// Position.m
//
#import "Sequence.h"
#import "Position.h"
#implementation Position
#synthesize Id;
#synthesize leftSequence;
#synthesize rightSequence;
-(id) copyWithZone: (NSZone *) zone {
Position *positionCopy = [[Position allocWithZone: zone] init];
positionCopy.Id = Id;
positionCopy.leftSequence = [leftSequence copy];
positionCopy.rightSequence = [rightSequence copy];
return positionCopy;
}
#end
//
// Sequence.h
//
#import <Foundation/Foundation.h>
#interface Sequence : NSObject <NSCopying> {
NSInteger numberOfFrames;
NSString *imageNameScheme;
NSString *endFrame;
}
-(id) copyWithZone: (NSZone *) zone;
#property (nonatomic, assign) NSInteger numberOfFrames;
#property (nonatomic, copy) NSString *imageNameScheme;
#property (nonatomic, copy) NSString *endFrame;
#end
//
// Sequence.m
// MCIT
//
#import "Sequence.h"
#implementation Sequence
#synthesize numberOfFrames;
#synthesize imageNameScheme;
#synthesize endFrame;
-(id) copyWithZone: (NSZone *) zone {
Sequence *sequenceCopy = [[Sequence allocWithZone: zone] init];
sequenceCopy.numberOfFrames = numberOfFrames;
sequenceCopy.imageNameScheme = imageNameScheme;
sequenceCopy.endFrame = endFrame;
return sequenceCopy;
}
#end
Works like a charm now thanks all. :D
If your intent is to make this a copyable class, then you need to declare that it conforms to the NSCopying protocol like so:
#interface Node: NSObject <NSCopying> {
Falling to declare the protocol can cause other objects to believe that the class is uncopyable even if it has a copyWithZone: method.