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

El Tomato
  • 6,479
  • 6
  • 46
  • 75
  • 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 https://developer.apple.com/devcenter/ios/index.action. – Joe Nov 14 '12 at 18:19

1 Answers1

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

Community
  • 1
  • 1
Joe
  • 56,979
  • 9
  • 128
  • 135
  • 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, – El Tomato Nov 14 '12 at 19:10
  • 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;` – Joe Nov 14 '12 at 19:17