Objective-c error with subclass not finding superclass interface - ios

I've been working on an app that uses a collection view, and I'm creating a custom view cell (Category View Cell) which is a subclass of UICollectionViewCell. I also wanted to create a subclass of the custom view cell (LinkCell). I've been searching for a while now and I cannot find why I am getting the error "Cannot find interface declaration of 'CategoryViewCell', superclass of 'LinkCell'"
//CategoryViewCell.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#class ViewController;
#interface CategoryViewCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *image;
#property (nonatomic) ViewController *parentView;
#property (nonatomic) NSString *cellName;
#end
//CategoryViewCell.m
#import "CategoryViewCell.h"
#implementation CategoryViewCell
#end
//LinkCell.h
#import <UIKit/UIKit.h>
#import "CategoryViewCell.h"
#import "PJP Webview.h"
#interface LinkCell : CategoryViewCell //Error here
#property (nonatomic) NSString *username;
#property (nonatomic) NSString *password;
#property (nonatomic) NSString *urlToLink;
#property (nonatomic) NSString *urlToLinkS;
#property (nonatomic) NSString *urlToLinkP;
#property (nonatomic) NSString *urlToLinkT;
#property (nonatomic) NSString *body;
-(IBAction)celltapped:(id)sender;
#end
//LinkCell.m
#import "LinkCell.h"
#implementation LinkCell
#synthesize urlToLink, username, password, image, cellName, parentView;
-(IBAction)celltapped:(id)sender {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *launchString = [NSString stringWithFormat:#"hasLaunched%#Before", self.cellName];
BOOL hasLaunchedCellBefore = [userDefaults boolForKey:launchString];
if (!hasLaunchedCellBefore) {
// first time launch code
hasLaunchedCellBefore = TRUE;
[userDefaults setBool:hasLaunchedCellBefore forKey:launchString];
[userDefaults synchronize];
PJP_Webview *vc = [parentView.storyboard instantiateViewControllerWithIdentifier:#"vc"];
vc.currentCell = self;
[parentView presentViewController:vc animated:YES completion:nil];
}
else {
}
}
#end
Could someone please show me where my error is?

I just solved it; ViewController.h uses the CategoryViewCell and I imported the .h and used the #class for the ViewController property on CategoryViewCell.h. Only the #class was necessary; I guess some sort of circular dependency was created. The weird part was that CategoryViewCell.h didn't have the errors, its subclass did. Thanks to anyone who answered!

Choose file CategoryViewCell.m and check it's added to target or not.

Related

Copying one NSString into other one Xcode

I have a problem in Xcode.I am trying to copy one NSString into other one but is not working.
The original NSString pageTitle is on the RestViewController:
This is my RestViewContoller.h:
#import <UIKit/UIKit.h>
#import "Restaurant.h"
#interface RestViewController : UIViewController
#property (strong, nonatomic) IBOutlet UILabel *TitleLabel;
#property (strong, nonatomic) IBOutlet UILabel *DescriptionLabel;
#property (strong, nonatomic) IBOutlet UIImageView *ImageView;
#property (nonatomic, strong) Restaurant *DetailModal;
#property (nonatomic, strong) NSString *pageTitle;
#property (nonatomic, strong) NSString *pageDescription;
#end
The NSString I want to save the data is on the RestViewController:
#property (nonatomic, strong) Restaurant *DetailModal;
but is a Restaurant class type.
My Restaurant class is:
Restaurant.h:
#import <Foundation/Foundation.h>
#interface Restaurant : NSObject
#property (nonatomic, copy) NSString *title;
#property (nonatomic, copy) NSString *desc;
#property (nonatomic, copy) NSString *image;
- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image;
#end
Restaurant.m:
#import "Restaurant.h"
#implementation Restaurant
- (instancetype)init:(NSString *)title descripiton:(NSString *)description image:(NSString *)image {
self = [super init];
if (self != nil) {
self.title = title;
self.desc = description;
self.image = image;
}
return self;
}
#end
And finally the place where I want to copy this NSStrings is my RestViewController.m:
#import "RestViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface RestViewController ()
#end
#implementation RestViewController
- (void)viewDidLoad {
[super viewDidLoad];
_pageTitle = #"Test";
_DetailModal.title = _pageTitle;
NSLog(#"_DetailModal.title: %#", _DetailModal.title);
}
#end
The problem is when I see the result of the
NSLog(#"_DetailModal.title: %#", _DetailModal.title);on the console,
it puts me:2016-11-21 11:42:05.407 MadEat[3667:104028]
_DetailModal.title: (null)
What can I do? I know I have a low level of Xcode, but I need your help please. Thank you very much.
Initailize the instance of DataModel First.
Then proceed with assignment
_DetailModal = [[DetailModal alloc] init:_pageTitle descripiton:#"Desc" image:#"image"];
There is a instance method defined already to do the task.
After this also you can alter the value by
_DetailModal.title = #"title_of_yours"
If you have to use detailmodal, you must alloc the restaurant class object (detailmodal). Then only the objects can be used.
detailmodal=[[restaurant alloc]init];
detailmodal.title=_pagetitle;
Before you can use a object, you have to create it first.
DetailModal *detailModal = [DetailModal new];
That way the object will be created and values stored to the class properties will be reserved.
Try:
_DetailModal.title = [[NSString alloc] initWithString: _pageTitle];
(Did not test this)
plz chek you _DetailModal is not nil
if it's nil then
first you need to alloc the memory for you DetailModal Class object
like this
DetailModal *detailModal = [DetailModal new];
then after set the value you want
detailModal.title = _pageTitle;

No visible #interface for 'NSString' declares the selector URLEncodeUsingEncoding

i am new to ios and objected C.
I am trying to encode a url, bit i am getting the error
/Users/zupportdesk/Desktop/MyIOSApps/ZDticketSystem/ZDticketSystem/ViewController.m:313:52:
No visible #interface for 'NSString' declares the selector
'URLEncodeUsingEncoding:'
#import "ViewController.h"
#import "AppDelegate.h"
#import "ApplicationEnvironmentURL.h"
#import "AFNetworking/AFNetworking.h"
#import "HHAlertView/HHAlertView.h"
#import "MBProgressHUD/MBProgressHUD.h"
#import "AppDelegate.h"
#interface ViewController ()<UITextFieldDelegate, UIActionSheetDelegate> {
ApplicationEnvironmentURL *applicationEnvironment;
NSString *BASEURL;
NSString *global_profileId;
AppDelegate *delegate;
}
#property (nonatomic, strong) NSString *username;
#property (nonatomic, strong) NSString *password;
#end
#implementation ViewController
--- defaults methods here
- (void)setupProfileidURL:(NSString *)profileToken {
NSString *encoded_profileToken = [profileToken URLEncodeUsingEncoding:NSUTF8StringEncoding];
NSString *user_login_url = [applicationEnvironment get_user_api_url];
BASEURL = [[user_login_url stringByAppendingString:#"Account/GetProfileDetails?profileToken="] stringByAppendingString:encoded_profileToken];
NSLog(#"Base URL: %#", BASEURL);
}
I am calling the method like this
[self setupProfileidURL:profileToken];
View Holder header file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UITextField *tv_username;
#property (weak, nonatomic) IBOutlet UITextField *tv_password;
#end
I want to implement this https://madebymany.com/blog/url-encoding-an-nsstring-on-ios . can some one help me with this url encoding.
You are getting this message
No visible #interface for 'NSString' declares the selector
'URLEncodeUsingEncoding:'
because URLEncodeUsingEncoding is a method which is defined in a Category and you are not using that category in your class thats why you are getting this message.
Also CFURLCreateStringByAddingPercentEscapes is deprecated from iOS so in place of it you can use stringByAddingPercentEncodingWithAllowedCharacters
Below is an example of of your function
- (void)setupProfileidURL:(NSString *)profileToken {
NSString *encoded_profileToken = [profileToken stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *user_login_url = [applicationEnvironment get_user_api_url];
BASEURL = [[user_login_url stringByAppendingString:#"Account/GetProfileDetails?profileToken="] stringByAppendingString:encoded_profileToken];
NSLog(#"Base URL: %#", BASEURL);
}
Also as per the #Larme comment you can create a category and use it in your application by importing it.

Cannot specify setter 'setTitle:' for properties of NSObject or WKInterfaceController

I am trying to set up a WKInterfaceTable. I followed Apples docs. My Label is not getting text and I get this error message in console:
Cannot specify setter 'setTitle:' for properties of NSObject or WKInterfaceController
How can I fix this? Objective-C please. Here s my code:
.h
#import <Foundation/Foundation.h>
#import <WatchKit/WatchKit.h>
#interface MainRowType : NSObject
#property (strong, nonatomic) IBOutlet WKInterfaceImage *image;
#property (strong, nonatomic) IBOutlet WKInterfaceLabel *title;
#end
.m
#import "MainRowType.h"
#implementation MainRowType
#end
.h
#import <WatchKit/WatchKit.h>
#import <Foundation/Foundation.h>
#import "MainRowType.h"
#interface WatchTableController : WKInterfaceController
#property (strong, nonatomic) IBOutlet WKInterfaceTable *tableView;
#property (strong, nonatomic) NSMutableArray *titleArray;
#end
.m
I call this method
- (void)configureTableWithData:(NSMutableArray*)dataObjects {
[self.tableView setNumberOfRows:[dataObjects count] withRowType:#"mainRowType"];
for (NSInteger i = 0; i < self.tableView.numberOfRows; i++) {
MainRowType* theRow = [self.tableView rowControllerAtIndex:i];
NSString *titleString = [dataObjects objectAtIndex:i];
[theRow.title setText:titleString];
}
}
Thanks
Your title property is the problem. You can't name a property "title" in those classes. Change the property name and you should be all set.

#class and #import on another view controller, what does it means?

I am newbie on Xcode, and trying to figure out more about coding in xcode.
So, I am trying to learn more about models (models operation) on objective C.
I am confused in #Class declaration in PhotoViewController.h and .m Files
as you may see below, I already imported Photo.h on appdelegate.m and also PhotoViewController.m files
the objective from my tutorial is PhotoViewController.m files can recognize self.photo.filename
But, why it has to add #Class and #property in PhotoViewController.h files?
isnt #import command is already enough? what does #Class means and why it has to include #property too?
note : I tried to put a comment (//) on #class , but xcode tell me that photo property not found, and when I put added comment (//) on property
PhotoViewController.m file also messed up with unrecognized photo property.
I dont quite understand, the use of #class and #import at the same time, plus declaring #property photo
here is Photo.m
#import "Photo.h"
#implementation Photo
-(id)init
{
self = [super init];
return self;
}
#end
and
Photo.h
#import <Foundation/Foundation.h>
#interface Photo : NSObject
#property (weak, atomic) NSString *title;
#property (strong, nonatomic) NSString *detail;
#property (strong, nonatomic) NSString *filename;
#property (strong, nonatomic) NSString *thumbnail;
#end
Appdelegate.m
#import "AppDelegate.h"
#import "FeedTableViewController.h"
#import "ProfileViewController.h"
#import "FavoritesViewController.h"
#import "Photo.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Photo *photo= [[Photo alloc]init];
photo.title = #"Demo Photo";
photo.detail = #"This is a demo photo";
photo.filename = #"demo.png";
photo.thumbnail = #"demo-thumb.png";
return YES;
}
#end
PhotoViewController.h Files
#import <UIKit/UIKit.h>
#class Photo;
#interface PhotoViewController : UIViewController
#property (weak, nonatomic) NSString *imageFileName;
#property (weak, nonatomic) NSString *imageTitle;
#property (strong, nonatomic) Photo *photo;
#end
PhotoViewController.m Files
#import "PhotoViewController.h"
#import "UIImageView+AFNetworking.h"
#import "Photo.h"
#implementation PhotoViewController
-(void)viewDidLoad {
// self.title = self.imageTitle;
UIImageView *imageView = [[UIImageView alloc] init];
[imageView setImageWithURL:[UIImage imageNamed:self.photo.filename]];
imageView.frame = CGRectMake(10,10,300,300);
[self.view addSubview:imageView];
UILabel *imageTitleLabel = [[UILabel alloc] init];
imageTitleLabel.text = self.imageTitle;
imageTitleLabel.frame = CGRectMake(11,320,300,40);
[self.view addSubview:imageTitleLabel];
}
#end
#class Photo defines the existence of class Photo to PhotoViewController.h allowing you to declare photo property.
Photo property is later used in PhotoViewController.m to to access the instance variable photo like this self.photo or [self photo]
You could have put #import "Photo.h" in your PhotoViewController.h but it is cleaner this way :)
#property
It is for replacement of getter method, whenever you want to get the value you have to declare that variable as property, so that need not to write getter method separately,
you should implement #synthesize also in Photo.m, #synthesize will work as setter method.

How to save UITextField Text to NSString?

In my main view controller, I have a UITextField, and I am trying to save the text input into it to a NSString in my Homework model(class).
Homework.h
#import <Foundation/Foundation.h>
#interface Homework : NSObject
#property (nonatomic, strong) NSString *className;
#property (nonatomic, strong) NSString *assignmentTitle;
#end
Homework.m
#import "Homework.h"
#implementation Homework
#synthesize className = _className;
#synthesize assignmentTitle = _assignmentTitle;
#end
In my assignmentViewController, I create an object of type Homework and try to set it equal to whatever is entered into the the UITextField when the Save Button is pressed.
Assignment View Controller
#import <UIKit/UIKit.h>
#import "Homework.h"
#interface Assignment : UIViewController {
}
#property(nonatomic) IBOutlet UITextField *ClassNameField;
#property(nonatomic) IBOutlet UILabel *ClassNameLabel;
#property (weak, nonatomic) IBOutlet UIButton *SaveButton;
#property (nonatomic, strong) Homework *homeworkAssignment;
- (IBAction)Save:(UIButton *)sender;
#end
AssignmentViewController.m
- (IBAction)Save:(UIButton *)sender {
self.homeworkAssignment.className = self.ClassNameField.text;
NSLog(#"SaveButtonPressed %#", self.homeworkAssignment.className);
}
The NSLog prints out that className is (null). Can anyone help me figure out what I am doing wrong? This is my first ever iOS app (besides Hello World).
Edit: This is using ARC
Edit: I tried changing
self.homeworkAssignment.className = self.ClassNameField.text; to
self.homeworkAssignment.className = #"TEST";
and the log still shows (Null).
Double check you properly linked ClassNameField outlet and that you're initializing homeworkAssignment. Something like.-
self.homeworkAssignment = [[Homework alloc] init];
By the way, you should consider using camelCase notation for your variable names :)
Well to be honest the first steps are always hard but you should learn it the right way, héhé
First of all synthesize this way:
#synthesize labelAssignmentTitle,labelClassName;
or
#synthesize labelAssignmentTitle;
#synthesize labelClassName;
there is no need to do the following:
#synthesize className = _className;
#synthesize assignmentTitle = _assignmentTitle;
Now if you initialize the right way from the the start you'll find it a lot easier later!
HomeWork.h
#interface HomeWork : NSObject
#property (nonatomic, strong) NSString *className;
#property (nonatomic, strong) NSString *assignmentTitle;
-(id)initWithClassName:(NSString *)newClassName andAssignmentTitle:(NSString*)newAssigmentTitle;
HomeWork.m
#implementation HomeWork
#synthesize assignmentTitle,className;
-(id)initWithClassName:(NSString *)newClassName andAssignmentTitle:(NSString*)newAssigmentTitle {
self = [super init];
if(self){
assignmentTitle = newAssigmentTitle;
className = newClass;
}
return self;
}
#end
ViewController.m
- (IBAction)saveIt:(id)sender {
HomeWork *newHomeWork = [[HomeWork alloc]initWithClassName:[labelClassName text]andAssignmentTitle:[labelAssignmentTitle text]];
}
Because of this, you directly make a newHomeWork object with the parameters given by your two UITextFields.
Now print it out in your logmessage and see what happends ;)

Resources