swift 2中的layoutAttributesForElements函数给出了错误警告

时间:2016-09-09 15:04:37

标签: uicollectionviewlayout swift3

我正在将我的项目从swift 2转换为swift 3,我很难理解以下函数的错误:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
   var attributes = [UICollectionViewLayoutAttributes]()
    if self.itemAttributes != nil {
        for section in self.itemAttributes {

            let filteredArray  =  (section as AnyObject).filtered(

                using: NSPredicate { (evaluatedObject, bindings) -> Bool in
                    return rect.intersects(evaluatedObject.frame)
                }
            ) as! [UICollectionViewLayoutAttributes]

            attributes.append(contentsOf: filteredArray)

        }
    }

   return attributes
}

self.itemAttributes是一个定义为:

的类属性

var itemAttributes : NSMutableArray!

sectionAttributes在另一个函数中设置:

override func prepare() {
  if self.collectionView?.numberOfSections == 0 {
        return
    }

    if (self.itemAttributes != nil && self.itemAttributes.count > 0) {
        for section in 0..<self.collectionView!.numberOfSections {
            let numberOfItems : Int = self.collectionView!.numberOfItems(inSection: section)
           for index in 0..<numberOfItems {
                if section != 0 && index != 0 {
                    continue
                }

                let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath(item: index, section: section))!
                if section == 0 {
                    var frame = attributes.frame
                    frame.origin.y = self.collectionView!.contentOffset.y
                    attributes.frame = frame
                }

                if index == 0 {
                    var frame = attributes.frame
                    frame.origin.x = self.collectionView!.contentOffset.x
                    attributes.frame = frame
                }
            }
        }
        return
    }

    if (self.itemsSize == nil || self.itemsSize.count != numberOfColumns) {
        self.calculateItemsSize()
    }

    var column = 0
    var xOffset : CGFloat = 0
    var yOffset : CGFloat = 0
    var contentWidth : CGFloat = 0
    var contentHeight : CGFloat = 0

    for section in 0..<self.collectionView!.numberOfSections {
        let sectionAttributes = NSMutableArray()

        for index in 0..<numberOfColumns {
            let itemSize = (self.itemsSize[index] as AnyObject).cgSizeValue
            let indexPath = IndexPath(item: index, section: section)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attributes.frame = CGRect(x: xOffset, y: yOffset, width: (itemSize?.width)!, height: (itemSize?.height)!).integral

            if section == 0 && index == 0 {
                attributes.zIndex = 1024;
            } else  if section == 0 || index == 0 {
                attributes.zIndex = 1023
            }

            if section == 0 {
                var frame = attributes.frame
                frame.origin.y = self.collectionView!.contentOffset.y
                attributes.frame = frame
            }
            if index == 0 {
                var frame = attributes.frame
                frame.origin.x = self.collectionView!.contentOffset.x
                attributes.frame = frame
            }

            sectionAttributes.add(attributes)

            xOffset += (itemSize?.width)!
            column += 1

            if column == numberOfColumns {
                if xOffset > contentWidth {
                    contentWidth = xOffset
                }

                column = 0
                xOffset = 0
                yOffset += (itemSize?.height)!
            }
        }
        if (self.itemAttributes == nil) {
            self.itemAttributes = NSMutableArray(capacity: self.collectionView!.numberOfSections)
        }
        self.itemAttributes .add(sectionAttributes)
    }

    let attributes : UICollectionViewLayoutAttributes = (self.itemAttributes.lastObject as AnyObject).lastObject as! UICollectionViewLayoutAttributes
    contentHeight = attributes.frame.origin.y + attributes.frame.size.height
    self.contentSize = CGSize(width: contentWidth, height: contentHeight)}

Xcode抛出Type of expression is ambiguous without more context 对于layoutAttributesForElements

中的return rect.intersects(evaluatedObject.frame)函数

2 个答案:

答案 0 :(得分:5)

在Swift 3中,NSMutableArray(或NSArray)的值类型已变为Any,这很难处理。

你最好尽可能快地将你的数组类型转换为Swift数组。

试试这个:

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var attributes = [UICollectionViewLayoutAttributes]()
    if let itemAttributes = self.itemAttributes as NSArray as? [[UICollectionViewLayoutAttributes]] {
        for section in itemAttributes {
            let filteredArray = section.filter {evaluatedObject in
                return rect.intersects(evaluatedObject.frame)
            }
            attributes.append(contentsOf: filteredArray)
        }
    }
    return attributes
}

但我建议您尽可能使用Swift原生ArrayDictionary

var itemAttributes: [[UICollectionViewLayoutAttributes]] = []

       var sectionAttributes: [UICollectionViewLayoutAttributes] = []

(有了这个,你可能还需要一些修复,但你很快就会找到它们。)

答案 1 :(得分:0)

OOPer的帮助下,我对代码进行了以下更改:

<?php
include './connect.php';
if (($_SERVER["REQUEST_METHOD"] == "POST") && (!empty($_POST['username'])) && (!empty($_POST['password']))) {
$postedUsername = $_POST['username'];
$postedPassword = $_POST['password'];
$userDatabaseFind = $database->login->findOne(array('username' => $postedUsername, 'password' => $postedPassword));
$storedUsername = $userDatabaseFind['username'];
$storedPassword = $userDatabaseFind['password'];

if (($postedUsername == $storedUsername) && ($postedPassword == $storedPassword)) {
    header('location:searchDetail.php');
} else {
    echo 'Error';
}
}
?>

<html>
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <style>
        #page-wrapper{
            width: 400px;
            margin: auto;
        }
        input[type="text"] {
            width: 450px;
        }
        input[type="password"]{
            width: 450px;
        }
        body{
            background-image: url(images/image2.jpg);
        }
    </style>
</head>
<body>
    <form class="form-horizontal" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
        <fieldset>
            <legend>Signin Form</legend>
            <div id="page-wrapper">
                <div class="form-group">
                    <label for="inputEmail" class="col-lg-2 control-label">Username</label>
                    <div class="col-lg-10">
                        <input type="text" class="form-control" id="inputEmail" placeholder="Username" name="username" >
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputPassword" class="col-lg-2 control-label">Password</label>
                    <div class="col-lg-10">
                        <input type="password" class="form-control" id="inputPassword" placeholder="Password" name="password">
                    </div>
                </div>
                <button type="submit" class="btn btn-success" name="button">Submit</button>
            </div>
        </fieldset>
    </form>

</body>

<?php
include './connect.php';
session_start();
if (($_SERVER["REQUEST_METHOD"] == "POST") && (!empty($_POST['username'])) && (!empty($_POST['password']))) {
$postedUsername = $_POST['username'];
$postedPassword = $_POST['password'];
$userDatabaseFind = $database->login->findOne(array('username' => $postedUsername, 'password' => $postedPassword));
$storedUsername = $userDatabaseFind['username'];
$storedPassword = $userDatabaseFind['password'];

if (($postedUsername == $storedUsername) && ($postedPassword == $storedPassword)) {
    $_SESSION['username'] = $storedUsername;
    if($_SESSION['username'] == 'admin'){
         header('location:searchDetail.php');
    }
    else{
        echo 'Hello world';
    }

} else {
    echo 'Error';
}
}
?>