No visible #interface for 'InstanceName' declares the selector' - automatic-ref-counting

My name is Bob. I code for the Parks Department of NYC. I'm starting to learn Objective C and have a "Semantic Issue." I must have a blind spot, because I checked all the following code and don't see the problem. Thank you for ant help you can give
"No visible #interface for 'Greeter' declares the selector 'setGreetingText::"
#import <Foundation/Foundation.h>
#import "Greeter.h"
int main (int argc, const char * argv[])
{
#autoreleasepool {
Greeter* myGreeter = [[Greeter alloc] init];
// error on this line
[myGreeter setGreetingText: #"Hello Objective-C!!" : #"Hello VIP Objective-C!!"];
[myGreeter issueGreeting];
}
return 0;
}
======================================
import "Greeter.h"
import "NewGreeter.h"
#implementation Greeter
NewGreeter *VTS;
- (NSString*) greetingText
{
return [self greetingText];
}
- (void) setGreetingText:(NSString*) newText01
andThisToo:(NSString*) newText02
{
greetingText = newText01;
[VTS setNewGreetingText: newText02];
}
- (void) issueGreeting
{
NSLog(#"%#", greetingText);
}
#import "NewGreeter.h"
#implementation NewGreeter
- (NSString*) NewGreetingText
{
return greeting;
}
- (void) setNewGreetingText:(NSString*) newText
{
greeting = newText;
}
- (void) issueNewGreeting
{
NSLog(#"%#", [self NewGreetingText]);
}
#end
#import <Foundation/Foundation.h>
#import "NewGreeter.h"
#interface Greeter : NSObject
{
NSString *greetingText;
}
- (void) setGreetingText:(NSString*) newText01
andThisToo:(NSString*) newText02;
- (NSString*) greetingText;
- (void) setGreetingText:(NSString*) newText;
- (void) issueGreeting;
#import <Foundation/Foundation.h>
#interface NewGreeter : NSObject
{
NSString *greetingText;
}
- (NSString*) NewGreetingText;
- (void) setNewGreetingText:(NSString*) newText;
- (void) issueNewGreeting;
#end
#import <Foundation/Foundation.h>
#interface NewGreeter : NSObject
{
NSString *greetingText;
}
- (NSString*) NewGreetingText;
- (void) setNewGreetingText:(NSString*) newText;
- (void) issueNewGreeting;
#end

You implement the method named -setGreetingText:andThisToo:, but you're trying to use it incorrectly. You need to change your call to:
[myGreeter setGreetingText: #"Hello Objective-C!!" andThisToo:#"Hello VIP Objective-C!!"];
Notice the andThisToo bit.
If you're only trying to set your object's properties, then you don't need to expose custom methods for that. Instead, you could do:
// In Greeter.h
#interface Greeter: NSObject
#property NSString *greetingText;
#end
// In Greeter.m
#implementation Greeter
#synthesize greetingText = _greetingText; // Create a backing instance variable
#end
This creates an instance variable setter method named setGreetingText and an instance variable getter named greetingText. I'd recommend you read up on creating object properties.

Related

Assigning property to subclass of RLMObject

I'm trying to use a subclass of a RLMObject as a base class for a property of other subclass of RLMObject, with no luck. Is this a known restriction, or is there a workaround for it?
test.m:
#import <XCTest/XCTest.h>
#import <Realm/Realm.h>
#interface Base : RLMObject; #end
#interface Derived : Base; #end
#interface User : RLMObject
#property Base *obj;
#end
#implementation Base; #end
#implementation Derived; #end
#implementation User; #end
#interface rlmrlmTests : XCTestCase; #end
#implementation rlmrlmTests
- (void) testOK {
User *user = [[User alloc] init];
user.obj = [[Base alloc] init]; // OK
[[RLMRealm defaultRealm] transactionWithBlock:^{
[[RLMRealm defaultRealm] addObject:user];
}];
}
- (void) testZFail {
User *user = [[User alloc] init];
user.obj = [[Derived alloc] init]; // ERROR
[[RLMRealm defaultRealm] transactionWithBlock:^{
[[RLMRealm defaultRealm] addObject:user];
}];
}
#end
will get an exception saying:
error: -[rlmrlmTests testZFail] : failed: caught "RLMException", "Can't set object of type 'Derived' to property of type 'Base'"
That's a current limitation of Realm. It doesn't support polymorphism on relations.
If possible, I'd avoid inheritance in this case. You could reach that by altering your object-relational mapping: You could add a further discriminator column to your Base class and have relations to classes which contain further attributes and wouldn't inherit anymore from the Base class. If you have just one subclass, then a discriminator column is eventually not needed.
#interface Base : RLMObject
#property NSString *discriminator;
#property BaseExtensionA *extA;
#property BaseExtensionB *extB;
#end
#interface BaseExtensionA : RLMObject
// the properties of the prior `Derived` would go here
#end
#interface BaseExtensionB : RLMObject
// …
#end

trouble sending data to detailview

I am using collection view to get data from my server display some of it in a cell and then I have a segue to the destination view controller I got the image to update correctly but for some reason the text is not going to my UITextView
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//DetailSegue
if ([segue.identifier isEqualToString:#"DetailSegue"]) {
ICBCollectionViewCell *cell = (ICBCollectionViewCell *)sender;
NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
ICBDetailViewController *dvc = (ICBDetailViewController *)[segue destinationViewController];
path = [paths objectAtIndex:indexPath.row];
Path = [path objectForKey:#"path"];
title = [titles objectAtIndex:indexPath.row];
Title = [title objectForKey:#"title"];
sku = [SKUs objectAtIndex:indexPath.row];
Sku = [sku objectForKey:#"SKU"];
longDescrip = [longDescription objectAtIndex:indexPath.row];
LongDescrip = [longDescrip objectForKey:#"longDescrip"];
LongDescrip =#"Hello World";
NSLog(#"Descrip =%#",LongDescrip);
NSString *iconTitle =[NSString stringWithFormat:#"%#.png",Sku];
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fullPath = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:#"/%#",iconTitle]]; //add our image to the path
dvc.img = [[UIImage alloc] initWithContentsOfFile:fullPath];
dvc.title = Title;
//UITextView *descrip = (UITextView *)[cell viewWithTag:120];
[dvc.descrip setText:#"Hello"];
}
}
I am not sure if it has something to do with the fact that the object being sent to is a UITextView and I am sending it a String
or if I have something hooked up wrong
here is the .m and .h for the detailController also
the .h
#import <UIKit/UIKit.h>
#interface ICBDetailViewController : UIViewController
#property(weak) IBOutlet UIImageView *imageView;
#property (strong) UIImage *img;
#property(weak) IBOutlet UITextView *descrip;
#end
the .m
#import "ICBDetailViewController.h"
#interface ICBDetailViewController ()
#end
#implementation ICBDetailViewController
#synthesize imageView, img, descrip;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.imageView.image = self.img;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Hopefully somebody will see what I have missed for two days
after again reviewing it all I noticed i missed the outlet in the .h and forgot to set it show in the ViewDidLoad of the .m I also missed hooking upthe referencing outlet to my txtView

Annotation not dragging

MapPin.h :
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
#interface MapPin : NSObject<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
#property (nonatomic,readwrite,assign)CLLocationCoordinate2D coordinate;
- (id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description;
-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
#end
and here's the MapPin.m
#import "MapPin.h"
#implementation MapPin
#synthesize coordinate;
-(NSString *)subtitle{
return nil;
}
-(NSString *)title{
return nil;
}
-(id)initWithCoordinates:(CLLocationCoordinate2D)location placeName:(NSString *)placeName description:(NSString *)description{
coordinate=location;
return self;
}
-(CLLocationCoordinate2D)location{
return coordinate;
}
-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate{
coordinate=newCoordinate;
}
#end
In the ViewController.h i've adopted the MKMapViewDelegate protocol.
and here's the ViewController.m :
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
MKPinAnnotationView *pin = (MKPinAnnotationView *)[self.MyMap dequeueReusableAnnotationViewWithIdentifier:#"myPin"];
if(pin == nil){
pin=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:#"myPin"];
}
else{
pin.annotation=annotation;
}
pin.draggable=YES;
return pin;
}
//Add Location button
- (IBAction)AddPin:(id)sender {
CLLocationCoordinate2D center;
center.latitude=Locate.location.coordinate.latitude;
center.longitude=Locate.location.coordinate.longitude;
MKCoordinateRegion region;
region.center=center;
region.span.latitudeDelta=0.2f;
region.span.longitudeDelta=0.2f;
MapPin *myPin = [[MapPin alloc]initWithCoordinates:self.MyMap.centerCoordinate placeName:#"LoMinder!" description:#"Set Location for LoMinder!"];
[_MyMap addAnnotation:myPin];
}
//Dropping the pin:
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
//Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
if (newState == MKAnnotationViewDragStateStarting)
{
[_MyMap setRegion:myRegion animated:YES];
}
if(newState == MKAnnotationViewDragStateDragging)
{
myRegion.center=annotationView.annotation.coordinate;
myRegion.span.latitudeDelta=0.2f;
myRegion.span.longitudeDelta=0.2f;
[_MyMap setRegion:myRegion animated:YES];
}
if(newState==MKAnnotationViewDragStateEnding)
{
arrived=self.MyMap.centerCoordinate;
center=annotationView.annotation.coordinate;
myRegion.center=center;
myRegion.span.latitudeDelta=0.2f;
myRegion.span.longitudeDelta=0.2f;
[_MyMap setRegion:myRegion animated:YES];
}
}
/////
I've also imported the MapPin.h into the ViewController.
When I run my app, I should press the Add Location button in ord er to get the annotation pin, I press the button, the annotation pin appears on the map, but the problem is that when i try to drag it, it seems to be un-draggable.
Please people some help.
Thank you :)
Solved by changing the title method in the MapPin.m
Make it return anything but nil.

block inside NSOPeration subclass crashing in the device but not in the simulator

I have been struggling with this piece of code for a while and I just don't know why it happens that when running in the device ... the app crashes with an EXC_BAD_ACCESS Error but when running in the simulator it runs fine.
The scenario: A subclass of NSOperation that makes an async connection with NSURLConnection and gets custom data. When finished, it calls the block with the downloaded data.
Here is the .h file:
#interface FileDownloader : NSOperation <NSURLConnectionDataDelegate>
typedef void (^CompletionBlockForFile)(NSData *);
- (id)initWithCompletionBlock:(CompletionBlockForFile)block;
#end
and the .m file:
#interface FileDownloader ()
#property (strong, nonatomic) CompletionBlockForFile completionBlock;
#property (strong, nonatomic) NSMutableData *downloadedData;
- (void)downloadFileWithCompletionBlock:(CompletionBlockForFile)block;
#end
#implementation FileDownloader
#synthesize downloadedData = _downloadedData;
#synthesize completionBlock = _completionBlock;
- (id)initWithCompletionBlock:(CompletionBlockForFile)block
{
self = [super init];
if (self) {
_completionBlock = block;
}
return self;
}
- (void)main
{
if (self.isCancelled) return;
if (_completionBlock) {
[self downloadFileWithCompletionBlock:_completionBlock];
}
}
- (void)downloadFileWithCompletionBlock:(CompletionBlockForFile)block
{
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
[connection start];
});
}
... delegate methods of NSURLConnection
#end
And Finally, the method that adds the operation object to the queue at the MainViewController Class:
- (void)viewDidLoad
{
[super viewDidLoad];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
FileDownloader *fileDownloader = [[FileDownloader alloc] initWithCompletionBlock:^(NSData *data){ // <----- HERE IS THE EXC_BAD_ACCES ERROR JUST WHEN RUNNING IN THE DEVICE !!! :S
NSLog(#"%#", data);
}];
[queue addOperation:fileDownloader];
}
Can anybody explain me what am I doing wrong? And is it correct to put strong for the block var in the property? Why not assign? Or weak ?
Thanks in advance :)
Blocks should be copied and not retained.
Change
#property (**strong**, nonatomic) CompletionBlockForFile completionBlock;
to
#property (**copy**, nonatomic) CompletionBlockForFile completionBlock;
and it should work

XCode 4 - #private

I don't know if it's some setting I accidentally ticked, but tell me how to fix it please:
Whenever I create a new Obj-C class, it automatically looks like:
#import <Foundation/Foundation.h>
#interface MathUtilities : NSObject {
**#private**
}
#end
That line is automatically inserted. It never was there before, but something is not adding it. My files also now come with init and dealloc methods. Did something happen? Also, shouldn't it be importing Cocoa instead of Foundation?
This is XCode 4
There is nothing to fix. XCode is creating stubs for you to fill out your code into. It's a time saver, thats all. It should be generating a header and implementation stub file for you, which you can extend like so:
Your header file (MathUtilities.h):
#import <Foundation/Foundation.h>
#interface MathUtilities : NSObject {
#private:
NSNumber * num;
}
- (void) doSomeWork;
#end
Your implementation file (MathUtilities.m) :
#import "MathUtilities.h"
#implementation MathUtilities
- (id) init {
self = [super init];
if(self) {
// Initialization code here.
}
return self;
}
- (void) dealloc {
[super dealloc];
}
- (void) doSomeWork {
return;
}
#end

Resources