Assertion in Helper Class Ignored - ios

I have a test case and a helper class. In the helper class I want to use asserts too like here:
MainTests.h
#import <SenTestingKit/SenTestingKit.h>
#interface MainTests : SenTestCase
#end
MainTests.m
#import "MainTests.h"
#import "HelperClass.h"
#implementation MainTests
- (void)testExample {
HelperClass *helperClass = [[HelperClass alloc] init];
[helperClass fail];
}
#end
HelperClass.h
#import <SenTestingKit/SenTestingKit.h>
#interface HelperClass : SenTestCase
- (void)fail;
#end
HelperClass.m
#import "HelperClass.h"
#implementation HelperClass
- (void)fail {
STFail(#"This should fail");
}
#end
Sidenote: I had to make the helper class a subclass from SenTestCase to being able to access the assertion macros.
The assertion from the helper class is ignored. Any ideas why? How can I use assertions in helper classes?

I had this same problem today and came up with a hack that worked for my purposes. Poking into the SenTestCase macros, I noticed that they call [self ...] on the helper but didn't trigger the asserts. So, wiring up the source class to the helper got it working for me. Changes to your question classes would look like:
MainTests.h
#import <SenTestingKit/SenTestingKit.h>
#interface MainTests : SenTestCase
#end
MainTests.m
#import "MainTests.h"
#import "HelperClass.h"
#implementation MainTests
- (void)testExample {
// Changed init call to pass self to helper
HelperClass *helperClass = [[HelperClass alloc] initFrom:self];
[helperClass fail];
}
#end
HelperClass.h
#import <SenTestingKit/SenTestingKit.h>
#interface HelperClass : SenTestCase
- (id)initFrom:(SenTestCase *)elsewhere;
- (void)fail;
#property (nonatomic, strong) SenTestCase* from;
#end
HelperClass.m
#import "HelperClass.h"
#implementation HelperClass
#synthesize from;
- (id)initFrom:(SenTestCase *)elsewhere
{
self = [super init];
if (self) {
self.from = elsewhere;
}
return self;
}
- (void)fail {
STFail(#"This should fail");
}
// Override failWithException: to use the source test and not self
- (void) failWithException:(NSException *) anException {
[self.from failWithException:anException];
}
#end
It is entirely possible that additional overrides are needed for more advanced functionality, but this did the trick for me.

Related

Message Forwarding is not working (Objective-C)

I am writing a sample code in order to understand message forwarding in Objective C (iOS).
I have two classes (class A and class B). I want to create an instance of class B and set a class A instance variable to it. I am calling the Forward Invocation method (Message Forwarding) in the following code.
// ViewController.h
// TestInvocation
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
// ViewController.m
// TestInvocation
#import "ViewController.h"
#import "TestInvocation.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[TestInvocation testRun];
[super viewDidLoad];
}
#end
// TestInvocation.h
// TestInvocation
#import <Foundation/Foundation.h>
#interface TestInvocation : NSObject
{}
+(void)testRun;
#end
// TestInvocation.m
// TestInvocation
#import "TestInvocation.h"
#import "ClassB.h"
#import "ClassA.h"
#implementation TestInvocation
+(void)testRun
{
ClassB* diplomat = [[ClassB alloc] init];
NSLog(#"value = %d",[diplomat value]);// Error shows up here on running:
//No visible #interface for 'ClassB' declares the selector 'value'
}
#end
// ClassA.h
// TestInvocation
#import <Foundation/Foundation.h>
#interface ClassA : NSObject
{
int value;
}
#property(readwrite,assign) int value;
#end
// ClassA.m
// TestInvocation
#import "ClassA.h"
#implementation ClassA
#synthesize value;
#end
// ClassB.h
// TestInvocation
#import <Foundation/Foundation.h>
#interface ClassB : NSObject
{}
#end
// ClassB.m
// TestInvocation
#import "ClassB.h"
#import "ClassA.h"
#implementation ClassB
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
return [ClassA instanceMethodSignatureForSelector:aSelector];
}
-(void)forwardInvocation:(NSInvocation *)anInvocation
{
ClassA* negotiate = [[ClassA alloc] init];
negotiate.value = 15;
[anInvocation invokeWithTarget:negotiate];
}
#end
I am expecting the above code to work. But instead I get the following build time error:
ARC Semantic Issue
TestInvocation.m:19:35: No visible #interface for 'ClassB' declares the selector 'value'
Class B should have the property in interface at least.
But you can make it #dynamic in implementation if you want to call forwardInvocation.
#interface ClassB : NSObject
#property (readwrite, assign) int value;
#end
#implementation ClassB
#dynamic value;
...
#end
I think it should work for you.

Property Inheritance in Objective C

I want to inherit my base class properties and methods which will be used by my several derived classes. I want these properties and methods to be exactly protected so that they will only be visible in derived class and not to any external class. But it always gives me some errors.
#interface BasePerson : NSObject
#end
#interface BasePerson ()
#property (nonatomic, strong) NSMutableArray<Person*>* savedPersons;
#property (nonatomic) BOOL shouldSavePerson;
#end
#interface DerivedPerson1 : BasePerson
#end
#implementation DerivedPerson1
- (instancetype)init
{
if (self = [super init]) {
self.savedPersons = [NSMutableArray array];
self.shouldSavePerson = NO;
}
return self;
}
It always gives me an error that
Property 'savedPersons' not found on object of type 'DerivedPerson1 *'
Property 'shouldSavePerson' not found on object of type 'DerivedPerson1 *'
How i can make use of inheritance in Objective C, I don't want savedPersons and shouldSavePerson properties to be visible to external classes. I only want them to visible in my base class and all the derived classes.
Any help will be great. Thanks
This is not something that the objectiveC really support. There are some ways though. So lets see.
If you put a property in the source file class extension then it is not exposed and you can not access it in the subclass either.
One way is to put all of the subclasses into the same source file as the base class. This is not a good solution at all as you do want to have separate files for separate classes.
It seems logical to import the BaseClass.m in the SubClass source file but that will produce a linker error saying that you have duplicate symbols.
And the solution:
Separate the extension into a separate header. So you have a MyClass
Header:
#import <Foundation/Foundation.h>
#interface MyClass : NSObject
#end
Source:
#import "MyClass.h"
#import "MyClassProtected.h"
#implementation MyClass
- (void)foo {
self.someProperty = #"Some text from base class";
}
#end
Then you create another header file (only the header) MyClassProtected.h which has the following:
#import "MyClass.h"
#interface MyClass ()
#property (nonatomic, strong) NSString *someProperty;
#end
And the subclass MyClassSubclass
Header:
#import "MyClass.h"
#interface MyClassSubclass : MyClass
#end
And the source:
#import "MyClassSubclass.h"
#import "MyClassProtected.h"
#implementation MyClassSubclass
- (void)foo {
self.someProperty = #"We can set it here as well";
}
#end
So now if the user MyClassSubclass he will not have the access to the protected property which is essentially what you want. But the downside is the user may still import MyClassProtected.h after which he will have the access to the property.
Objective-C doesn't have member access control for methods, but you can emulate it using header files.
BasePerson.h
#interface BasePerson : NSObject
#property (strong,nonatomic) SomeClass *somePublicProperty;
-(void) somePublicMethod;
#end
BasePerson-Private.h
#import "BasePerson.h"
#interface BasePerson ()
#property (nonatomic, strong) NSMutableArray<Person*>* savedPersons;
#property (nonatomic) BOOL shouldSavePerson;
#end
BasePerson.m
#import "BasePerson-Private.h"
...
DerivedPerson1.h
#import "BasePerson-Private.h"
#inteface DerivedPerson1 : BasePerson
...
#end
Now any class that #imports BasePerson.h will only see the public methods. As I said though, this is only emulating access control since if a class #imports *BasePerson-Private.h" they will see the private members; this is just how C/Objective-C is.
We can achieve using #protected access specifier
#interface BasePerson : NSObject {
#protected NSMutableArray *savedPersons;
#protected BOOL shouldSavePerson;
}
DerivedPerson1.m
#implementation DerivedPerson1
- (instancetype)init
{
if (self = [super init]) {
self->savedPersons = [NSMutableArray array];
self->shouldSavePerson = NO;
}
return self;
}
#end
OtherClass.m
#import "OtherClass.h"
#import "BasePerson.h"
#implementation OtherClass
- (void)awakeFromNib {
BasePerson *base = [[BasePerson alloc]init];
base->savedPersons = #[];//Getting Error. Because it is not a subclass.
}
#end

Delegate methood not called

I am not sure why my delegate method not being called. below is my code .Please tell me where i have made a mistake.
I have made all the set up correctly and i am sure i have made a silly mistake.my delegate method not being called. Any help will be greatly appreciated
LSFiveViewController.h
#protocol OneToOne;
#protocol OneToOne <NSObject>
#optional
-(void)dismissPopOverlsfive;
#end
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
#import "TimeTableViewController.h"
#class TimeTableViewController;
#interface LearningSessionFiveDetailsViewController : UIViewController<UITextFieldDelegate>
{
NSString *bookingString;
}
#property(nonatomic,weak)id<OneToOne>OneToOne;
#end
**LSFiveViewController.m**
#synthesize sessionAndDateLabelObj,OneToOne
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)BookSingleSessionAction:(id)sender
{
if (self.OneToOne && [self.OneToOne respondsToSelector:#selector(dismissPopOverlsfive)])
{
[self.OneToOne dismissPopOverlsfive];
}
}
#end
TimeTableViewController.h
#import "OneToOneViewController.h"
#import "LearningSessionFiveDetailsViewController.h"
//other codes
#property(nonatomic,strong)LearningSessionFiveDetailsViewController *LSFiveObj;
#end
TimeTableViewController.m
#import "TimeTableViewController.h"
#import "LearningSessionFiveViewController.h"
#import "UIAlertView+Blocks.h"
- (void)viewDidLoad
{
LSFiveObj=[[LearningSessionFiveDetailsViewController alloc]init];
self.learnSearchFiveClassObj.OneToOne=self;
}
-(void)dismissPopOverlsfive
{
This method is not being called
}
Please Put this
self.LSFiveObj.OneToOne=self;
Instead of
self.learnSearchFiveClassObj.OneToOne=self;
and add OneToOne Protocol in your class
This line here:
self.learnSearchFiveClassObj.OneToOne=self;
should be
self.LSFiveObj.OneToOne=self;
In general property names should not start with a capital letter, thats only for classes.

iOS accessing NSObject class from my viewController

I'm trying to access to another class from my viewcontoller but is not working:
viewcontroller.h
#import <UIKit/UIKit.h>
#class firstClass; //nsobject class
#interface ViewController : UIViewController
{
firstClass *firstclass;
}
#property (retain,nonatomic) LEMZfirstClass *firstclass;
---
firstClass.h:
#import "LEMZViewController.h"
#interface firstClass : NSObject
{
ViewController *viewController;
}
#property (retain,nonatomic) ViewController *viewController;
-(void)doSomenthing;
firstClass.m:
#synthesize viewController;
-(void)doSomenthing
{
viewController.firstclass=self;
viewController.outPutLabel.text=#"This is my Label";
}
viewcontroller.m:
#synthesize firstclass;
- (void)viewDidLoad
{
[super viewDidLoad];
[firstclass doSomenthing];
}
it compiles with no errors but the label is never updated and for that matter the first class is never call it all. What I'm doing wrong? I'll really appreciate your help.
A few things I'm noticing:
Generally you would have the ViewController class handle updating its own UI elements, not another class.
Where is your outPutLabel variable? Is it created by code or an IBOutlet that is wired up in InterfaceBuilder?
Before you can call something on firstclass, you must create it. Something like this:
firstclass = [[firstClass alloc] init];
[firstclass doSomenthing];
The viewController.firstclass=self; line would be redundant then.
Your firstClass.h
#import <Foundation/Foundation.h>
#interface firstClass : NSObject
+(NSString *)doSomenthing; //Instance Class
#end
firstClass.m
#import "firstClass.h"
#implementation firstClass
+(NSString *)doSomenthing
{
return #"This is my Label";
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "firstClass.h"
#interface ViewController : UIViewController
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[firstClass doSomenthing];
outPutLabel.text=[firstClass doSomenthing];;
// Do any additional setup after loading the view, typically from a nib.
}
Note: Here I am using instance class. Before you work this code you must study about Instance class.

iOS method in data controller isn't being called

I am trying to call a method in my data controller object to load the data for my application, but for some reason it is not being called. Below is what I have done to initialize it. Any help would be greatly appreciated.
ViewController:
header file:
#import <UIKit/UIKit.h>
#class DetailViewController;
#class DataController;
#import <CoreData/CoreData.h>
#import "JointCAD.h"
#interface TableViewController : UITableViewController {
}
#property (nonatomic, assign) DataController *dataController;
#end
implementation file:
#import "TableViewController.h"
#import "DataController.h"
#implementation TableViewController
#synthesize dataController;
- (void)viewDidLoad {
[dataController refreshData];
}
#end
Data Controller:
header file:
#import <Foundation/Foundation.h>
#import "JointCAD.h"
#import "JointCADXMLParser.h"
#import "TFHpple.h"
#interface DataController : NSObject {
TFHpple *xpathParser;
}
- (void)refreshData;
- (void)initXMLParser;
- (void)noCallsMessage;
- (void)noInternetMessage;
#end
implementation file:
#import "DataController.h"
#implementation DataController
XMLParser *xmlParser;
- (void)refreshData {
NSLog("Some Method");
}
Is 'dataController' Object being set by some other class? - I believe that's why you have set it as a property? Right?
If No, then Remove the property,#synthesize of 'dataController' and try simple allocation of your 'dataController' object and then try calling your method.
Hope it helps.
You either need to initialize "DataController" prior to actually calling one of it's methods, or you need to make the method, "refreshData" a class by changing it's "-" to a "+".
If you need an instance callback instead. You need to rewrite "viewDidLoad" like this:
- (void)viewDidLoad {
DataController *dataController = [[DataController alloc] init];
[dataController refreshData];
}
And get rid of the property declaration of dataController because you haven't initialized it. If you would prefer a property declaration instead, simply allocate the viewcontroller prior to calling a function from it.
- (void)viewDidLoad {
dataController = [[DataController alloc] init];
[dataController refreshData];
}
One last thing to note is that I (and probably Ray) assume that you're using a storyboard configuration. If you are using a xib configuration, you need to add initWithNibName: to each initialization of the view controller.
I hope that's helpful!

Resources