: error: expected member name or ';' after declaration specifiers; 'friend' is a keyword in Objective-C++ #property( - objective-c++

expected member name or ';' after declaration specifiers; 'friend' is a keyword in Objective-C++
#property(nonatomic, readonly) LinphoneFriend *friend;
expected identifier; 'friend' is a keyword in Objective-C++
- (instancetype)initWithFriend:(LinphoneFriend *) friend;

It appears that friend is a keyword in C++:
The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears.
via https://en.cppreference.com/w/cpp/language/friend
Unfortunately, your options are:
rename your variables (eg to linphoneFriend)
switch to Objective-C

Related

is there a way to define a variable thats accessible to some classes and not others?

In objective-c, the access to variables is limited to three types which is #public , #private , #protected (default) and #package .. these access modifiers allow us to access the variable through 4 situations in order :
1- access the variable from anywhere.
2- access the variable only inside the class.
3- access the variable from anywhere in the class and its subclasses.
4- access the variable from anywhere in the framework.
my question is: is there a way to define a variable which is accessible to some classes and not others ? (i.e. customised scope for variables)
What you're asking for is C++'s friend keyword. Friend classes in Objective-C discusses the topic.
You can use class extensions to create more flexible access control:
// MyClass.h
#interface MyClass : SomeSuperclass {
int ivar;
}
#end
// MyClass-Custom.h
#include "MyClass.h"
#interface MyClass () {
int anotherIvar;
}
#end
Now anotherIvar will be accessible only to code that #includes MyClass-Custom.h. You can create more class extensions on the same class to get additional access groups.
You would have to write your own setter and getter methods.
- (id) get_abc_value:(id)from {
if ([from isKindOfClass:[SomeRespectedClass class]]) {
return abc;
}
return nil;
}

Why am I getting a "no visible #interface message for 'NSDecimalNumber' declares the selector" for my method?

I must be missing something simple here. Anyway, I started out by just making a regular function,
NSDecimalNumber* aa(NSMutableString *string)
{code}
which I would then call by pressing a button like so:
- (IBAction)parse:(id)sender {
string1=[NSMutableString stringWithFormat:#"%#", screen.text];
NSDecimalNumber *output=aa(string1);}
(screen.text is from a label) However, partway into it, I realized that the function can't use variables from the rest of my viewcontroller.m class (and vice-versa), so I decided to implement the function as a method instead. Here's what I did. First, I added this to viewcontroller.h,
+ (NSDecimalNumber*) aa:(NSMutableString*) string;
#property (nonatomic, strong) NSDecimalNumber *number; //the number I'm working with
synthesized my property, changed my function declaration to this,
+ (NSDecimalNumber*) aa:(NSMutableString*) string
and attempted to call it like this,
NSDecimalNumber *output=[[NSDecimalNumber alloc] aa:string1];
With that attempt, I got two errors -- "No visible #interface for 'NSDecimalNumber' declares the selector 'aa,'" and "instance variable 'number' accessed in class method."
So I tried again with an instance method. Changed the +'s to -'s and instead called the method with
NSDecimalNumber *output;
[output aa:string1];
That corrected the second error but not the first one. I can't figure out why it isn't recognizing the method in the #interface. Also, those weren't the only things I've tried changing -- I've been playing around with multiple ways to call the method, but nothing seems to work. Any ideas?
This function call:
NSDecimalNumber *output=[[NSDecimalNumber alloc] aa:string1];
..is attempting to call aa an instance of NSDecimalNumber. I don't think that's what you want, isn't your aa method a member of your class? Also, you're not calling a class initializer (although you don't need to, since your method is static so long as its definition starts with +):
// MyClass method definition
+ (NSDecimalNumber*) aa:(NSMutableString*) string
// Called with
NSDecimalNumber *output=[MyClass aa:string1];
--UPDATE--
To address the "instance variable" error, you need to make the method an instance method. Change + in definition to - and call it thusly:
// MyClass method definition
- (NSDecimalNumber*) aa:(NSMutableString*) string
// Call it like this _if calling from within MyClass only_ (hence, "self")
NSDecimalNumber *output = [self aa:string];
If you want to add methods to NSDecimalNumber, you need to use a category. Your code adds a method to your view controller subclass.

Is defining a property & synthesize for a variable in a singleton class allowed?

Defining a property & synthesize for a variable in a singleton class allowed?,like below
in interface,
#property(nonatomic,assign)NSInteger value;
and in implementation file,
#synthesize value;
or we just have to declare a variable like below,
#interface SingletonDataClass : NSObject
{
NSInteger value;
}
Anything that you can do with your regular classes you can do with singletons.
There is no language concept called "singleton", it is just a common usage pattern of regular Objective C classes. What makes a class a singleton is the way you ensure its instantiation happens only once, i.e. your own supporting code.

How to use accessors within the same class in Objective C?

I have a few properties defined in my header file like so
#property (assign) bool connectivity_N;
#property (assign) bool isConnected_N;
In my implementation file I have an init and the synthesized properties like so
#implementation Map
#synthesize connectivity_N;
#synthesize isConnected_N;
a init to set the initial values like so
-(id) init
{
if( (self=[super init]) )
{
//initialise default properties
self.connectivity_N=NO;
self.isConnected_N=NO;
}
return self;
}
I'm running into an error that states Error: accessing unknown 'connectivity_N' class method. In this public method within the class
+(bool) isConnectable:(directions) theDirection{
bool isTheDirectionConnectable= NO;
switch (theDirection) {
case north:
isTheDirectionConnectable= self.connectivity_N;
break;
I'm not sure why is this so as I'm trying to grab the value of the property. According to the apple developer documentation "The default names for the getter and setter methods associated with a property are propertyName and setPropertyName: respectively—for example, given a property “foo”, the accessors would be foo and setFoo:"
That has given me a clue that I've done something wrong here, I'm fairly new to objective C so would appreciate anyone who spends the time to explain this to me.
Thanks!
You've defined isConnectable: as a class method by prefixing it with +. Probably you want it to be an instance method -- start it with a minus sign - instead.
You can't access self within class methods, because there is no object instance.
Although self exists in class methods, it doesn't refer to an object instance -- there is no object -- so you can't access object properties. (Thanks to Dave DeLong for the correction.)

iPhone static libraries: How to hide instance variable

I'm creating a static library to share using the following guide:
http://www.amateurinmotion.com/articles/2009/02/08/creating-a-static-library-for-iphone.html
In one of the functions, I return a "SomeUIView" which is a subclass of UIView and is defined in the public header, however I don't want to expose the internal instance variable of SomeUIView in the public header.
I've tried using categories for a private internal header file for SomeUIView, but I keep running into "Duplicate interface declaration for class 'SomeUIView'".
Does anyone know how to do this?
Thanks!
Categories and extensions can't add instance variables to a class. I'd go for the PIMPL idiom here - use a private implementation object:
// header
#class MyObjImpl;
#interface MyObj {
MyObjImpl* impl;
}
#end
// implementation file:
#interface MyObjImpl {
id someIvar;
}
// ...
#end
// ... etc.
This also keeps your public interface stable in case you want to add something for internal use.
The "duplicate interface" comes from missing parentheses in the second interface declaration:
// header:
#interface MyObj
// ...
#end
// implementation file:
#interface MyObj () // note the parentheses which make it a class extension
// ...
#end
You may also use the Objective-C 2 feature known as "Associative reference".
This is not really object-oriented API, but you can add/remove object to another object by using some simple functions of the runtime:
void objc_setAssociatedObject(id object, void * key, id value)
Sets the value or remove it when value is nil.
id objc_getAssociatedObject(id object, void * key)
Retrieve the value for specified key.
Note that this is also a mean to add "instance variable" to existing object when implementing a category.
Key is s simple pointer to private variable that you can declare as a module private by using:
static char SEARCH_INDEX_KEY = 0;