2

I'm very new to Objective-C. I've developed several dozens of desktop applications with VB.NET and several dozens more with REAL Stupid for Macs. I've read several hard-cover books and PDF books over Objective-C functions. They only talk about how to create a function with integers. I want to go beyond integers. For example, the following simple VB.NET function involves a string, returning true or false (boolean). It's quite simple and straightforward.

Function SayBoolean (ByVal w As String) As Boolean
If w = "hello" Then
    Return True
Else
    Return False
End if
End Function

The following function returns a string (the file extension) with a string (a file path).

Function xGetExt(ByVal f As String) As String
    'Getting the file extension    
    Dim fName1 As String = Path.GetFileName(f)
    Dim fName2 As String = Path.GetFileNameWithoutExtension(f)
    Dim s As String = Replace(Replace(fName1, fName2, ""), ".", "")
    Return s
End Function

So how do you specify string parameters and return a boolean or a string in creating a function with Objective-C? Objective-C is terribly difficult for me so far.

Thank you for your help.

Tom

1
  • I am not sure what books you've read that only cover passing integers, but they sound terrible. You should find more than enough resources here developer.apple.com/devcenter/ios/index.action. Commented Nov 14, 2012 at 18:19

1 Answer 1

2

Example 1

//The return value is a boolean (BOOL)
- (BOOL)sayBoolean:(NSString*)w //w is the string parameter
{
    //Use isEqualToString: to compare strings
    return [w isEqualToString:@"hello"]; 
}

Example 2

//The return value is a string
- (NSString*)xGetExt:(NSString*)f
{
   //pathExtension exists as an NSString method in a category
   // and returns a string already.
   return [f pathExtension]; 
}

Why you need to use isEqualToString: Understanding NSString comparison

Sign up to request clarification or add additional context in comments.

2 Comments

How do you use the string you've specified with the If loop like If w = "hello" Then End if If I write If (w == @"hello"){ } I get an error. Thank you,
It returns a BOOL so there is no reason for an if statement. If you want an if statement then it would be if([w isEqualToString:@"hello"])return YES;else return NO;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.