A couple questions from my feeble mind about Objective-C

In summary, the conversation discusses the issue with returning an array from a method in Objective-C and suggests changing the return type to char*.
  • #1
Whovian
652
3
Okay. A few questions.

Is there an escape character similar to '%s' which uses an NSString instead of a C-style string, so I could do something like NSLog(@"Hello, %N!",/*Insert NSString* here*/)?

I tried the following code:

Code:
#import <Foundation/Foundation.h>

@interface Greeter : NSObject
{
    char name[256];
}
-(void)setname : (char[256])newname;
-(void)greet;
-(char[256])getname;
@end

@implementation Greeter
-(void)setname : (char[256])newname
{
    for (int i = 0;i<256;i++)
    {
        name[i]=newname[i];
    }
}
-(void)greet
{
    printf("Hello, %s!",name);
}
-(char[256])getname
{
    return name;
}
@end

It's not happy with my return name; expression. My mistake's probably really obvious, but what is it? Maybe I should have it return char* instead?
 
Last edited:
Technology news on Phys.org
  • #2
The issue with this code is that you are trying to return an array (char[256]) from the getname method. Arrays cannot be returned as values in Objective-C, so you need to change your return type to char*. The following should work:-(char*)getname { return name;}
 

FAQ: A couple questions from my feeble mind about Objective-C

1. What is Objective-C?

Objective-C is a high-level, object-oriented programming language primarily used for developing applications for Apple's macOS and iOS operating systems. It is a superset of the C programming language, meaning that it includes all of the features of C and adds additional syntax and features for object-oriented programming.

2. What are some key features of Objective-C?

Some key features of Objective-C include dynamic binding, message passing, and the use of protocols for object communication. It also uses a syntax that is easy to read and learn, making it a popular language for beginners.

3. Can I use Objective-C to develop applications for non-Apple platforms?

While Objective-C was originally created for Apple's operating systems, it can also be used for developing applications for other platforms such as Linux and Windows. However, it may require some additional tools and libraries to make it compatible with these systems.

4. How does Objective-C compare to other programming languages?

Objective-C is often compared to other object-oriented programming languages such as Java and C++, as well as Apple's newer programming language, Swift. It has a similar syntax to these languages, but it also has its own unique features and style.

5. Is Objective-C still relevant in today's technology landscape?

While Swift has become the primary programming language for developing applications for Apple's platforms, Objective-C is still widely used and supported. Many existing applications and libraries are written in Objective-C, and it is still a useful language for developers to learn.

Back
Top