NsMutable Array search and conversion - objective-c

I have mutable array of class like :
#protocol CategoryModel #end
#interface CategoryModel : NSObject
#property (assign) int Id;
#property (strong, nonatomic) NSString* ShortString;
#property (strong, nonatomic) NSString* Description;
#property (strong, nonatomic) NSString* AppId;
#end
I want to get an Nsdata of the AppId , how to do that
and I want to search this NsMutableArry by Id

As far as I understand you want to get data from array by Id. You can use NSPredicate for this:
[myArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"Id == %d", yourID]];

Related

Objective-C - Wrong data binding?

I wonder if there is anything wrong in this code:
//MyViewController.h
#interface MyViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *aLabel;
#property (strong, nonatomic) NSString *myString;
#end
//MyViewController.m
#implementation MyViewController
-(void)setMyString:(NSString *)myString{
if (_myString != myString) {
_myString = myString;
self.myLabel.text = myString;
}
}
#end
Regards!
you need
![_myString isEqualToString: myString];
instead of
_myString != myString
PS:
also use
self->_myString
please make sure that you are using
self.myString = #"new String"
instead of
_myString = #"new String"
self.property is equal to [self setProperty]

Unable to add string into an mutable array

I am trying to upload assignments into a table in an different view controller. First when i click on the upload button this code runs.
- (IBAction)uploadAssignment:(id)sender {
nameOfAssignmentAsString = self.nameOfAssignment.text;
NSLog(#"%#", nameOfAssignmentAsString);
NSInteger row;
row = [self.subjectPicker selectedRowInComponent:0];
subjectOfAssignmentAsString = [subjectArray objectAtIndex:row];
NSLog(#"%#", subjectOfAssignmentAsString);
NSDate *deadline = [self.deadlinePicker date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
deadlineOfAssignmentAsString = [dateFormat stringFromDate:deadline];
NSLog(#"%#",deadlineOfAssignmentAsString);
HomeworkViewController *HVC;
[HVC.nameOfAssignmentInAnArray addObject:nameOfAssignmentAsString];
[HVC.SubjectOfAssignmentInAnArray addObject:subjectOfAssignmentAsString];
[HVC.deadlineOfAssignmentInAnArray addObject:deadlineOfAssignmentAsString];
NSLog(#"%#",HVC.nameOfAssignmentInAnArray.count);
}
then in my HomeworkViewController.h this is the code
#interface HomeworkViewController : UIViewController <UIGestureRecognizerDelegate,UITableViewDataSource,UITableViewDelegate> {
int i;
}
#property (strong, nonatomic) IBOutlet UIView *mainMenu;
#property (strong, nonatomic) IBOutlet UIButton *homeworkButton;
#property (strong, nonatomic) IBOutlet UIButton *uploadButton;
#property (strong, nonatomic) NSMutableArray *nameOfAssignmentInAnArray;
#property (strong, nonatomic) NSMutableArray *SubjectOfAssignmentInAnArray;
#property (strong, nonatomic) NSMutableArray *deadlineOfAssignmentInAnArray;
I did initialise it in my HomeworkViewController.m file. But for some reason the string is not being added into the array.
You declare HVC but never assign a value to it. Therefore it cannot hold anything in itself. But for some reason the designers of Objective-C decided it's not an error to send messages to nil objects, so this might not be noticed.

Restkit mapping relationship

Here is my json
{
"status":"ok",
"count":1,
"count_total":164,
"pages":164,
"posts":[{"id":2966,
"type":"post",
"title":"title here",
"content":"here content",
"date":"",
"attachments [
"comment_count":0,
"thumbnail":""]
}]
}
one class Attachments and one class articles
Attachments.h
#import <Foundation/Foundation.h>
#interface Attachments : NSObject
#property (strong, nonatomic) NSString *thumbnail;
#end
Attachments.m
#import "Attachments.h"
#implementation Attachments
#synthesize thumbnail;
#end
articles.h
#import <UIKit/UIKit.h>
#import "Attachments.h"
#interface articles : NSObject
#property (strong, nonatomic) NSString *title;
#property (strong, nonatomic) Attachments *attachments;
#property (strong, nonatomic) NSString *date;
#end
articles.m
#import "articles.h"
#implementation articles
#synthesize title;
#synthesize attachments;
#synthesize date;
#end
viewcontroller.h
#import <UIKit/UIKit.h>
#import <RestKit/RestKit.h>
#import "articles.h"
#import "postCell.h"
#interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, RKObjectLoaderDelegate> {
__weak IBOutlet UISearchBar *_searchBar;
__weak IBOutlet UITableView *_tableView;
}
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
#property (strong, nonatomic) NSMutableArray *post;
#end
in viewcontroller.m
RKObjectMapping* attachmentsMapping = [RKObjectMapping mappingForClass:[Attachments class]];
[attachmentsMapping mapKeyPathsToAttributes:#"thumbnail", #"thumbnail", nil];
RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[articles class]];
[postMapping mapKeyPathsToAttributes:#"title",#"title",#"date",#"date",nil];
[postMapping mapKeyPath:#"attachments" toRelationship:#"attachments" withMapping:attachmentsMapping];
[objectManager.mappingProvider setMapping:postMapping forKeyPath:#"posts"];
[self searchRequest];
- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
NSLog(#"objects[%d]", [objects count]);
[posts removeAllObjects];
for (articles *t in objects) {
[posts addObject:t];
}
[self.tableView reloadData];
}
i get this error
[RKObjectMappingOperation transformValue:atKeyPath:toType:] Failed
transformation of value at keyPath 'attachments'. No strategy for
transforming from '__NSArrayM' to 'Attachments'
What's wrong?
Thanks
Change
#property (strong, nonatomic) Attachments *attachments;
To
#property (strong, nonatomic) NSMutableArray *attachments;
The relationship mapping returns an array of data, even if there's only one.

Objective C serialize a collection to JSON/XML

I have a classes looking like this:
#interface AISlideItem: NSObject <NSCoding>
{
NSString* PlaceHolderName;
NSInteger PlaceHolderID;
}
#property (nonatomic, strong) NSString* PlaceHolderName;
#property (nonatomic) NSInteger PlaceHolderID;
#end
#interface AITitleSlideItem : AISlideItem
{
NSString* Title;
}
#property (nonatomic, strong) NSString* Title;
#end
#interface AIParagraphSlideItem : AISlideItem
{
NSMutableArray* Paragraphs;
}
#property (nonatomic, strong) NSMutableArray* Paragraphs;
#end
#interface AITextSlideItem : AISlideItem
{
NSString* Text;
}
#property (nonatomic, strong) NSString* Text;
#end
#interface AIPictureSlideItem : AISlideItem
{
NSMutableData* Picture;
}
#property (nonatomic, strong) NSMutableData* Picture;
#end
#interface AISlideContent : NSObject
{
NSString* LayoutName;
NSMutableArray* Items;
}
#property (nonatomic,strong) NSString* LayoutName;
#property (nonatomic,strong) NSMutableArray* Items;
#end
#interface ContentParaghraph : NSObject
{
NSInteger Level;
NSString* Text;
}
#property (nonatomic) NSInteger Level;
#property (nonatomic, strong) NSString* Text;
#end
I create a complex collection from these classes like this:
NSMutableArray* l = [[NSMutableArray alloc]init ];
AITitleSlideItem* tis1 = [[AITitleSlideItem alloc]init];
[tis1 setPlaceHolderName:#"I don't think we're using this..."];
[tis1 setPlaceHolderID:1];
[tis1 setTitle:#"This is a title"];
AITextSlideItem* tes1 = [[AITextSlideItem alloc]init];
[tes1 setPlaceHolderName:#"I don't think we're using this..."];
[tes1 setPlaceHolderID:2];
[tes1 setText:#"This is the description"];
AISlideContent* slide1 = [[AISlideContent alloc]init];
[slide1 setLayoutName:#"Title content"];
[[slide1 Items]addObject:tis1];
[[slide1 Items]addObject:tes1];
[l addObject:slide1];
[l addObject:slide1];
What i want to to serialize the NSMutableArray l into any portable format like json or xml.
Could you please post some code doing so?
Sincerely
Zoli
After further investigation i found nothing more suitable for my needs.
I decided to learn a bit more about JSON and create a function that creates the JSON structure manually. Even if it took 2-3 hours, it did worth all the effort.

iPhone Super/SubClass Example

I have a superclass Question:
import <Foundation/Foundation.h>
#import "Answer.h"
#interface Question : NSObject {
NSString* qId;
NSString* qTitle;
NSString* qNumber;
NSString* sectionId;
NSString* type;
Answer* answer;
}
#property (nonatomic, retain) NSString* qId;
#property (nonatomic, retain) NSString* qTitle;
#property (nonatomic, retain) NSString* qNumber;
#property (nonatomic, retain) NSString* sectionId;
#property (nonatomic, retain) NSString* type;
#property (nonatomic, retain) Answer* answer;
#end
#import "Question.h"
#implementation Question
#synthesize qId, qTitle, qNumber, sectionId, type, answer;
-(id)init
{
if (self = [super init])
{
// Initialization code here
answer = [[Answer alloc]init];
}
return self;
}
-(void) dealloc{
[answer release];
[super dealloc];
}
#end
I have several types of Question, one example is a Slider Question. I want this class to subclass Question:
#import <Foundation/Foundation.h>
#import "Question.h"
#interface SliderQuestion : Question {
(NSString*) min;
(NSString*) max;
}
#property (nonatomic, retain) NSString* min;
#property (nonatomic, retain) NSString* max;
#end
}
#import "SliderQuestion.h"
#implementation SliderQuestion
#synthesize min, max;
#end
Is this the correct way to subclass? Will SliderQuestion inherit the properties contained within Question?
SliderQuestion* s = [[SliderQuestion alloc]init];
NSLog(#"%#", s.qId); //is this valid
Do you really want min and max to be instances of NSString? It seems that floats would be more appropriate.
Also, scrap the () in (NSString *) to remove the warning/error message.
Finally, this is the appropriate way to subclass. An instance of SliderQuestion will inherit all properties and methods of the Question class (and NSObject as well)
Notice the ( and ) around the NSStrings, you might wanna remove those.