I have a c-function within a class:
#interface MyClass: NSObject
#end
#implementation MyClass
static void MyFunction(void) {
// Do some stuff.
}
#end
And I'd like to expose my function for testing:
#interface MyClass (testing)
static void MyFunction(void);
#end
But this leads to error:
Function 'MyFunction' has internal linkage but is not
definedclang(-Wundefined-internal)
Is there some way to expose this method?
Related
I have two objective-c classes HondaDealerShip and FordDealerShip. They both contain similar properties and methods, so I want to define a common protocol DealerShip and do some polymorphism.
The problem is this DealerShip needs to contain generic enum type properties, so that HondaDealerShip and FordDealerShip can have different concrete enum types.
So I want something like this,
#protocol DealerShip
#property (nonatomic, readonly) enum location;
-(void)printPriceOfModel:(enum)vehicleModel;
#end
typedef NS_ENUM(NSInteger, HondaLocation) {
HondaLocationSouthEast,
HondaLocationNorthWest
}
typedef NS_ENUM(NSInteger, HondaModel) {
Accord,
Civic
}
#interface HondaDealerShip: NSObject<DealerShip>
#property (nonatomic, readonly) HondaLocation location;
- (void)printPriceOfModel:(HondaModel)vehicleModel {
//print price here
}
#end
typedef NS_ENUM(NSInteger, FordLocation) {
FordLocationEast,
FordLocationWest
}
typedef NS_ENUM(NSInteger, FordModel) {
Mustang,
Focus
}
#interface FordDealerShip: NSObject<DealerShip>
#property (nonatomic, readonly) FordLocation location;
- (void)printPriceOfModel:(FordModel)vehicleModel {
//print price here
}
#end
If I have to do this in swift, I could use protocols with associated types like below
protocol DealerShip {
associatedtype Location
associatedtype Model
var location: Location { get }
func printPriceOfModel(model : Model)
}
enum HondaLocation: Int {
case sountEast
case northWest
}
enum HondaModel: Int {
case accord
case civic
}
struct HondaDealerShip: DealerShip {
var location: HondaLocation
func printPriceOfModel(model: HondaModel) {
//print
}
}
//same for FordDealerShip
can I do similar in objective-c?.
Nope, you cannot use enum with associated types in objective c. The language does not support it.
Class A is writen in Objective C and have a custom init fucntion
#interface A ()
....
#end
#implementation A
- (id)customInitImplementedInA
{
...
return self;
}
Class B inherits from Class A and uses this custom init in the following way:
#interface B : A ()
....
#end
#implementation B
+(instancetype)instanceB{
B *b = [[B alloc] customInitImplementedInA];
...
return b;
}
Now I would like to create class C writen in Swift that inharits from A and uses the same init function. How do I do that?
class C: A {
//How do I use customInitImplementedInA here?
}
You should be able to do this via super.methodname syntax.
class C: A {
init() {
super.customInitImplementedInA()
// Any extra initialization for C goes here.
}
}
If you inherit your swift class from Obj-C than your swift clas will have obj-c init().
if you need to call it you can include it to your swift init.
class C: A {
var message: B?
init(message: B) {
super.customInitImplementedInA()
self.message = message
}
}
Hi I am trying to learn Opps concept in Objective C but I know PHP so I took a program in which for public, private and protected mentioned bellow.
<?php
//Public properties and method can be inherited and can be accessed outside the class.
//private properties and method can not be inherited and can not be accessed outside the class.
//protected properties and method can be inherited but can not be accessed outside the class.
class one
{
var $a=20;
private $b=30;
protected $c=40;
}
class two extends one
{
function disp()
{
print $this->c;
echo "<br>";
}
}
$obj2=new two;
$obj2->disp(); //Inheritance
echo"<br>";
$obj1=new one;
print $obj1->c; //Outside the class
?>
So this I am trying to convert in Objective c code mentioned bellow.
#import <Foundation/Foundation.h>
#interface one : NSObject
{
#private int a;
#public int b;
#protected int c;
}
#property int a;
#property int b;
#property int c;
#end
#implementation one
#synthesize a,b,c;
int a=10;
int b=20;
int c=30;
#end
#interface two : one
-(void)setlocation;
#end
#implementation two
-(void)setlocation;
{
// NSLog(#"%d",a);
NSLog(#"%d",b);
// NSLog(#"%d",c);
}
#end
int main(int argc, const char * argv[]) {
#autoreleasepool {
// insert code here...
two *newtwo;
newtwo =[[two alloc]init];
//calling function
[newtwo setlocation];
}
return 0;
}
When I run the above code I am getting
2015-11-03 23:20:16.877 Access Specifier[3562:303] 0
Can some one resolve my problem.
This type of question has been asked before and there's a good explanation in the accepted answer for Private ivar in #interface or #implementation
In general I would recommend you avoid instance variables and use #property instead. Properties have the benefit of read-only/write controls, and free synthesized setters and getters (which if you're learning OOP concepts is a critical concept you should employ).
Properties are declared in the #interface part of an Obj-C file. For access control (according to the link) you have no public/private/protected keywords. All Obj-C methods (and by extension, properties) are public if they're defined in the .h file. If you want them "private" you define them in the the .m file using a class category:
//MyClass.m
#interface MyClass ()
#property(nonatomic, retain) NSString* myString;
#end
#implementation MyClass
#end
I need to get access to an object inside a C function similar to this few code
#interface MixerHostAudio () <UIApplicationDelegate>
#property (readwrite) int *alternativeOutput;
#end
#implementation MyCode
#synthesize alternative
void audioInputAvailable () {
alternative=1;
}
I get this error: " 'Use of undeclared identifier 'alternative' "
Any ideas about how can i solve it ?
You have to make your "MyCode" object available somewhere for your C glue function to pick up. For example, if you have a pointer to your MyCode object...
void audioInputAvailable(MyCode *myCodeObject){
myCodeObject.alternative = 1;
}
I'm developing an app for iOS using Xamarin.
In my solution i've added a binding project to include some native library, but I've some trouble writing the ApiDefinition.cs
This is the original library:
#class AccordionView;
#protocol AccordionViewDelegate <NSObject>
#optional
- (void)accordion:(AccordionView *)accordion didChangeSelection:(NSIndexSet *)selection;
- (void)accordion:(AccordionView *)accordion heightUpdate:(NSInteger)height;
#end
#interface AccordionView : UIView <UIScrollViewDelegate> {
[..]
}
- (void)addHeader:(id)aHeader withView:(id)aView setBool:(BOOL)arrow;
- (void)setOriginalSize:(CGSize)size forIndex:(NSUInteger)index;
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView;
- (void)touchDown:(id)sender;
This is how i writed the ApiDefinitions.cs
[BaseType (typeof(UIView))]
interface AccordionView : UIScrollViewDelegate
{
[Export ("initWithFrame:")]
IntPtr Constructor(RectangleF frame);
[Export ("addHeader:withView:setBool:")]
void AddHeader(NSObject aHeader, NSObject aView, bool arrow);
[Export ("touchDown:")]
void TouchDown(NSObject sender);
[Export ("setOriginalSize:forIndex:")]
void SetOriginalSize(SizeF size, uint index);
[Export ("scrollViewDidScroll:")]
void ScrollViewDidScroll(UIScrollView scrollView);
}
[BaseType (typeof(NSObject))]
[Model]
interface AccordionViewDelegate
{
[Export ("accordion:didChangeSelection:")]
void DidChangeSelection(AccordionView accordion, NSIndexSet selection);
[Export ("accordion:heightUpdate:")]
void HeightUpdate(AccordionView accordion, int height);
}
My problem is on the UIScrollViewDelegate, I don't know how to "translate" it.
Someone can help me? :)
Have you tried looking at the walk-through using Objective Sharpie?
http://docs.xamarin.com/guides/ios/advanced_topics/binding_objective-c/Walkthrough_Binding_objective-c_library/