El Tomato
El Tomato

Reputation: 6705

Objective-C Basics: Creating a Function with String

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

Upvotes: 2

Views: 206

Answers (1)

Joe
Joe

Reputation: 57179

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

Upvotes: 2

Related Questions